按行,替换指定列中等于值的值 [英] By row, replace values equal to value in specified column
问题描述
希望这是一个简单的.我似乎无法拼凑出一个答案.我有一个数据框.对于每一行,我都有需要更改为 NA 的值.需要为每一行更改的值并不相同.我想根据指定列中的值将每一行的值更改为 NA.
Hopefully this is an easy one. I just can't seem to piece together an answer. I have a data frame. For each row, I have values that I need to change to NA. It is not the same value that needs to be changed for every row. I want to change values to NA for each row based on a value that is in a specified column.
mydata = as.data.frame(rbind(c("AA","CC","BB","DC","CC"),c("CC","CC","BB","DC","BB"),c("BB","BB","BB","DC","DC")))
> mydata
V1 V2 V3 V4 V5
1 AA CC BB DC CC
2 CC CC BB DC BB
3 BB BB BB DC DC
#for each row, replace values that match the value in column 5 with NA
apply(mydata[,1:4], 1, function(x){
x[x %in% x$V5] = NA
})
期望的输出
> mydata
V1 V2 V3 V4 V5
1 AA NA BB DC CC
2 CC CC NA DC BB
3 BB BB BB NA DC
谢谢!
----更新----
使用下面来自 arvi1000 的代码非常适合将一行中的值与单列值进行比较.有没有办法做这样的事情,但将值与 2 列或更多列进行比较?
Using the code below from arvi1000 works great for comparing values in a row to a single column of values. Is there a way to do something like this but comparing the values to 2 or more columns?
当前代码
mydata[,1:4][mydata[,1:4]==mydata[,5]] <- NA
假设我还有第 6 列.按行,我想将不等于第 5 列或第 6 列中的值的值更改为 NA.
Let's say I also have a column 6. By row, I want to change values that do not equal values in columns 5 or 6 to NA.
mydata = as.data.frame(rbind(c("AA","CC","BB","DC","CC","AA"),c("CC","CC","BB","DC","BB","CC"),c("BB","BB","BB","DC","DC","BB")),stringsAsFactors=F)
> mydata
V1 V2 V3 V4 V5 V6
1 AA CC BB DC CC AA
2 CC CC BB DC BB CC
3 BB BB BB DC DC BB
期望的输出
> mydata
V1 V2 V3 V4 V5 V6
1 AA CC NA NA CC AA
2 CC CC BB NA BB CC
3 BB BB BB DC DC BB
我尝试这样做,但收到错误
I tried to do this, but received an error
mydata[,1:4][mydata[,1:4]==mydata[,5]|mydata[,6]] <- NA
Error in mydata[, 1:4] == mydata[, 5] | mydata[, 6] :
operations are possible only for numeric, logical or complex types
推荐答案
将 stringsAsFactors=F 添加到 as.data.frame.这是关键,因为 'CC'!='CC'
当它们是不同级别的不同因素时.
Add stringsAsFactors=F to as.data.frame. This is key because 'CC'!='CC'
when they are different levels of different factors.
mydata = as.data.frame(rbind(c("AA","CC","BB","DC","CC"),c("CC","CC","BB","DC","BB"),c("BB","BB","BB","DC","DC")),
stringsAsFactors=F)
那么:
mydata[,1:4][mydata[,1:4]==mydata[,5]] <- NA
瞧:
V1 V2 V3 V4 V5
1 AA <NA> BB DC CC
2 CC CC <NA> DC BB
3 BB BB BB <NA> DC
这篇关于按行,替换指定列中等于值的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!