希望这很简单。我似乎无法拼凑出答案。我有一个数据框。对于每一行,我都需要将值更改为NA。它与每行需要更改的值不同。我想根据指定列中的值将每行的值更改为NA。
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
谢谢!
---- ---- UPDATE
使用arvi1000中的以下代码非常适合将一行中的值与一列值进行比较。有没有办法做这样的事情,但比较两个或更多列的值?
当前代码
mydata[,1:4][mydata[,1:4]==mydata[,5]] <- NA
假设我也有一列6.按行,我想将第5列或第6列中不等于值的值更改为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
我试图这样做,但收到错误
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
答案 0 :(得分:1)
将stringsAsFactors = F添加到as.data.frame。这是关键,因为'CC'!='CC'
当它们是不同因素的不同层次时。
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
答案 1 :(得分:1)
另一种方法是使用apply:
mydata = as.data.frame(rbind(c("AA","CC","BB","DC","CC"),c("CC","CC","BB","DC","BB"),c("BB","BB","BB","DC","DC")))
mydata <- data.frame(t(apply(mydata,1,function(x) {
for ( i in 1:(ncol(mydata)-1)){
if ( x[i] == x[ncol(mydata)]) {
x[i] <- NA
}
}
return(x)
})))
输出:
> mydata
V1 V2 V3 V4 V5
1 AA <NA> BB DC CC
2 CC CC <NA> DC BB
3 BB BB BB <NA> DC