简体   繁体   中英

Fill in column with values depending on different column within data frame

I have two columns, a and b (lengths of the columns are about 85,000). b is empty and a is filled with a variety of values. If the values are in list c, then b needs to be filled with "Yes". If the values are in list d, then b needs to be filled with "No". If they are in neither list, then b can be left blank or filled with NA (doesn't really matter). Right now, I have a for loop with if, if else, and else statements. While this works, it is not quick (takes about 20 seconds). Is there any way to do this with vector operations to speed it up? Thanks in advance!

for (i in 1:length(a)){
    if(is.element(df$a[i],c) == TRUE){
      df$b[i] <- "Yes"
    }
    else if (is.element(df$a[i],d) == TRUE){
      df$b[i] <- "No"
    }
    else{
      df$b[i] <- NA
    }
  }

If you create two separate vectors, one of the indices where a is in c and one in the indices where a is in d.

in.c <- which(df$a %in% df$c)
in.d <- which(df$a %in% df$d)

Then you can update b according to those vectors

df$b[in.c] <- 'Yes'
df$b[in.d] <- 'No'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM