繁体   English   中英

将NA应用于满足R中条件的行

[英]Apply NA to the rows that meet a condition in R

我有一个像这样的data.frame:

 set.seed(126)
df <- data.frame(a=sample(c(1:100, NA), 10), b=sample(1:100, 10), c=sample(1:100, 10))

    a  b  c
1  65 48 19
2  46 15 80
3  NA 47 84
4  68 34 46
5  23 75 42
6  92 87 68
7  79 28 48
8  84 55  9
9  28 43 38
10 94 99 77
> 

我想编写一个将df$aNA所有列中的所有值都转换为NA的函数。但是,我不想只给bc NA的值,而是希望一个函数将所有如果满足条件is.na(a)is.na(a)中的所有列都为NA,无论列数如何。

我想你只是在找

df[is.na(df$a), ] <- NA
#     a  b  c
# 1  65 48 19
# 2  46 15 80
# 3  NA NA NA
# 4  68 34 46
# 5  23 75 42
# 6  92 87 68
# 7  79 28 48
# 8  84 55  9
# 9  28 43 38
# 10 94 99 77

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM