Your attempt was just overly complicated. All you needed was threshold <- c( .2 , .4 , .5 )[ df$track ] df$value <- pmax(threshold, df$value) df # desired outcome
Cheers On Thu, Feb 9, 2012 at 3:56 PM, Idris Raja <idris.r...@gmail.com> wrote: > # I have a dataframe in the following form: > > track <- c(rep('A', 3), rep('B', 4), rep('C', 4)) > value <- c(0.15, 0.25, 0.35, 0.05, 0.99, 0.32, 0.13, 0.80, 0.75, 0.60, 0.44) > df <- data.frame(track=factor(track), value=value) > > #> print(df) > #track value > #1 A 0.15 > #2 A 0.25 > #3 A 0.35 > #4 B 0.05 > #5 B 0.99 > #6 B 0.32 > #7 B 0.13 > #8 C 0.80 > #9 C 0.75 > #10 C 0.60 > #11 C 0.44 > > > # If any of the values are below a threshold value, I want to replace it > with the > # threshold value. The twist is that there is a different threshold value > for > # every track. > > # I tried something like this, but it's not working > > threshold <- list() > threshold['A'] <- 0.2 > threshold['B'] <- 0.4 > threshold['C'] <- 0.5 > > > for (track in levels(df$track)){ > df[df$track==track,]$outcome <- pmax(df[df$track==track,]$outcome, > threshold[track]) > } > # Warning messages: > # 1: In is.na(mmm) : is.na() applied to non-(list or vector) of type 'NULL' > # 2: In is.na(mmm) : is.na() applied to non-(list or vector) of type 'NULL' > # 3: In is.na(mmm) : is.na() applied to non-(list or vector) of type 'NULL' > > > #****************** > # Desired Results: > > #> print(df) > #track value > #1 A 0.20 # value changed > #2 A 0.25 > #3 A 0.35 > #4 B 0.40 # value changed > #5 B 0.99 > #6 B 0.40 # value changed > #7 B 0.40 # value changed > #8 C 0.80 > #9 C 0.75 > #10 C 0.60 > #11 C 0.50 # value changed > > > # Any ideas? Thanks for reading. > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.