R在给定多个条件下替换变量

crcmnpdw  于 2023-04-09  发布在  其他
关注(0)|答案(3)|浏览(125)

我的数据组织如下:

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.我想用0替换company == fordcompany == nissan行的每个car_total。什么命令可以完成此操作?
1.如果我的约束来自不同的列呢?例如,如果我想替换任何car_total,它的company == fordcolor == red为0呢?

gr8qqesn

gr8qqesn1#

正如你从注解中看到的,这可以作为一个标准的选择简洁地完成,但是有时逻辑向量会使事情更清晰。
假设您的 Dataframe 名为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

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

qq24tv8q

qq24tv8q2#

我喜欢使用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]
chy5wohz

chy5wohz3#

第一个问题:

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)
}

相关问题