Home > Software engineering >  R: Change the Contents of Column A based on Column B
R: Change the Contents of Column A based on Column B

Time:01-08

I am cleaning some data in R, and I am imputing different values for some outliers that are clearly not correct, so I am doing the following:

dat$colA[dat$colA > 10000] <- quantile(dat$colA, c(.95))

This changes the values of two columns. Now, I want to change the contents of another column based on what I changed here. For example, the above line changed the values for rows 24 and 676. Now, I want to impute a constant value in a different column for rows 24 and 676, but I don't want to hard code it. I'd like to perform some sort of indexing to do so. How can I do this in R?

In other words, I want to set colB to 1 for rows 24 and 676. How can I do this by referencing the values in colA?

CodePudding user response:

Create an index i telling where in colA are the changes to take place, then use and reuse the index many times as you want.

i <- which(dat$colA > 10000)
dat$colA[i] <- quantile(dat$colA, 0.95)
dat$colB[i] <- 1
  • Related