简体   繁体   English

R 满足零比率条件的子集列

[英]R subset columns that meets a condition of zero ratio

I have a dataset named df,我有一个名为 df 的数据集,

Here I want to get the columns (make a new dataset with them), that have a zero ratio of > %50在这里,我想获取零比率 > %50 的列(用它们创建一个新数据集)

df_new <- get columns where zero_ratio> %50

could you support?你能支持吗?

Thank You谢谢你

Try with colMeans :尝试使用colMeans

df_new <- df[, colMeans(df == 0, na.rm = TRUE) > 0.5]

With a reproducible example:有一个可重现的例子:

df <- data.frame(a = c(1, 2, 0, 1, 3), b = c(0, 0, 1, 0, 1), c = 0)
df_new <- df[, colMeans(df == 0, na.rm = TRUE) > 0.5]
df_new

#  b c
#1 0 0
#2 0 0
#3 1 0
#4 0 0
#5 1 0

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

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