-1

I have a data frame (150000 obs, 15 variables) in R and need to correct a subset of values of one variable (simply by multiplying by a constant) based on the value of another. What's an easy way to do this?

I though apply would work, but I'm not sure how to write the function (obviously can't multiply in the function) and qualifier:

df$RESULT <- df[apply(df$RESULT, 1, function(x * 18.01420678) where(SITE==1)), ]
Arun
  • 116,683
  • 26
  • 284
  • 387
user1357079
  • 79
  • 3
  • 8
  • 2
    Hi there! Please make your post reproducible by having a look at [**How to make a great reproducible example**](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for us to help you. Thank you. – Arun Apr 05 '13 at 14:49
  • Come up with a toy example with 4-5 rows, 2-3 columns as input. Also mention the expected output. – Nishanth Apr 05 '13 at 15:01

2 Answers2

4

you mean this?

dat      <- data.frame(x=1:10,y=sample(20,10))
constant <- 100
dat$y    <- ifelse(dat$x > dat$y, dat$y*constant, dat$y)
user1317221_G
  • 15,087
  • 3
  • 52
  • 78
2

You could use the capacity of "[" to do subsetting but for "correction" of a subset you need to use the logical expression that defines the subset on both sides of the assignment. Since you will then be working with only the values that need correction you do not use any further conditional function.

df[ df$SITE==1, "RESULT" ] <- df[ df$SITE==1, "RESULT"] * 18.01420678

In cases where the operation is to be done on large (millions) of cases or done repeatedly in simulations, this approach may be much faster that the ifelse approach

IRTFM
  • 258,963
  • 21
  • 364
  • 487