简体   繁体   中英

Replacing a value on a data.frame based on multiple conditions

This is somewhat a follow-up on a previous question where the idea is to replace a given value in data.frame based on multiple conditions.

Here is a simple example that works, with base R:

df <- data.frame( var1=c("k",2,NA,6,5,"K","k",1),
              var2=c(4,2,6,0,9,1,3,2),
              var3=c("d","d","d","d","d","d","n","f"))
df

    var1 var2 var3
1    k    4    d
2    2    2    d
3 <NA>    6    d
4    6    0    d
5    5    9    d
6    K    1    d
7    k    3    n
8    1    2    f

The objective is to modify the value var1==k & var3==n by say a factor of 9:

df$var2[df$var1=="k" & df$var3=="n"] <- df$var2[df$var1=="k" & df$var3=="n"]/9

However, the actual df of interest generates the error message stated in the above question. The answer provided therein, negate NA's with each condition, df$var1=="k" & !is.na(df$var1) solves the issue with ample lines of code.

My question: is there a simpler solution using let's say plyr ?

We can use data.table . Convert the 'data.frame' to 'data.table' ( setDT(df) ). We specify the logical condition in 'i', assign ( := ) the variable ('var2') as 'var2/9'. This would be efficient as it modifies in place.

library(data.table)
setDT(df)[!is.na(var1) & var1=='k' & var3=='n', var2 := var2/9]

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