R替换变量给定多个条件

时间:2016-02-03 23:53:17

标签: r indexing

我的数据组织如下:

year    company color   car_total
2000    toyota  red     873
2013    honda   red     737
2012    nissan  green   809
2002    toyota  blue    429
2000    nissan  green   861
2012    honda   red     742
2009    toyota  red     320
2010    ford    yellow  319
2000    ford    green   587
2011    nissan  blue    777
2014    ford    blue    32

我试图在给定多个条件的情况下替换列中的值。两种情况:

  1. 我想将car_total OR company == ford的每行company == nissan替换为0.什么命令可以完成此操作?

  2. 如果我的约束来自不同的列怎么办?例如如果我想替换任何car_total == ford OR company ==红色为0的color怎么办?

3 个答案:

答案 0 :(得分:1)

正如您从评论中看到的那样,这可以作为标准选择紧凑地完成。但有时逻辑向量会使事情变得更加清晰。

假设您的数据框名为df

redcars <- df$color == "red"
fords <- df$company == "ford"
ford_or_nissan = fords | df$company == "nissan" # or alternatively
ford_or_nissan = df$company %in% c("ford","nissan")

这为您提供了三个可用于选择所需行的向量

df$car_total[ford_or_nissan] <- 0
df$car_total[fords | redcars] <- 0

使用逻辑运算符,您可以根据需要构建复杂的选择。

答案 1 :(得分:1)

我喜欢使用data.table

# Replace car total with 0 when company = ford OR company = nissan
dt[company %in% c("ford","nissan"), car_total := 0]

# Replace any car_total with 0 when company = ford OR color = red
dt[company == "ford" | color == "red", car_total := 0]

答案 2 :(得分:-3)

关于你的第一个问题:

    year<-c(2000,2013,2012,2002,2000,2012,2009,2010,2000,2011,2014)
company<-c('toyota','honda','nissan','toyota','nissan','honda','toyota','ford','ford','nissan','ford')
color<-c('red','red','green','blue','green','red','red','yellow','green','blue','blue')
car_total<-as.integer(c(873,737,809,429,861,742,320,319,587,777,32))
df<-data.frame(year,company,color,car_total)
for (i in 1:nrow(df))
{
ifelse (df$company[i]=='ford', df$car_total[i]<-0, NA)
ifelse (df$company[i]=='nissan',df$car_total[i]<-0, NA)
}