[英]Convert 0s to NAs when that is the only value in a row. Keep all other 0s using R
I have a dataframe 'animals': 我有一个数据框“动物”:
ID A B C D
1 Bear 1 1 1 0
2 Tiger 0 0 0 0
3 Horse 1 0 1 0
4 Badger 0 0 0 1
5 Rabbit 1 1 0 1
6 Otter 0 0 0 0
7 Peacock 1 0 0 0
I would like to convert the zeros in rows only containing zeros to NAs but to leave other zeros as they are. 我想将仅包含零的行中的零转换为NA,但保留其他零。 I can do this as follows: 我可以这样做,如下所示:
animals$Result = rowSums(animals[2:ncol(animals)])
df = data.frame()
for(row in 1:nrow(animals)) {
row = as.data.frame(animals[row,])
if(row$Result == 0){
row[2:5] = NA
}
df = rbind(df,row)
print(row)}
df$Result = NULL
To obtain this: 要获得此:
ID A B C D
Bear 1 1 1 0
Tiger NA NA NA NA
Horse 1 0 1 0
Badger 0 0 0 1
Rabbit 1 1 0 1
Otter NA NA NA NA
Peacock 1 0 0 0
However, I feel there should be an easier way to do this. 但是,我觉得应该有一个更简单的方法来做到这一点。 Is there? 在那儿? Thank you! 谢谢!
We can do this without a loop by creating a logical vector based on counting the number of 0's per each row with rowSums
. 我们可以通过基于rowSums
计算每行0的数目来创建逻辑向量,而无需循环。 Based on that subset the dataset without the first column and assign the rows that satisfy the condition to NA 基于该子集,不使用第一列的数据集将满足条件的行分配给NA
df1[!rowSums(df1[-1]!=0), -1] <- NA
df1
# ID A B C D
#1 Bear 1 1 1 0
#2 Tiger NA NA NA NA
#3 Horse 1 0 1 0
#4 Badger 0 0 0 1
#5 Rabbit 1 1 0 1
#6 Otter NA NA NA NA
#7 Peacock 1 0 0 0
Here is second base R method that uses Reduce
to find the rows to set to NA and then lapply
to loop through the variables, with replace
doing the replacement work. 这是第二个基本R方法,该方法使用Reduce
来找到要设置为NA的行,然后应用lapply
遍历变量,而replace
进行替换工作。
# find rows to set to NA
nas <- !Reduce("|", df[-1])
# run through relevant variables, setting desired elements to NA
df[-1] <- lapply(df[-1], replace, nas, NA)
This returns 这返回
df
ID A B C D
1 Bear 1 1 1 0
2 Tiger NA NA NA NA
3 Horse 1 0 1 0
4 Badger 0 0 0 1
5 Rabbit 1 1 0 1
6 Otter NA NA NA NA
7 Peacock 1 0 0 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.