[英]How to use sapply function on specific columns?
I have a function that removes columns without a given number of unique values.我有一个 function 删除没有给定数量的唯一值的列。 This command does it for the whole data frame.
该命令对整个数据帧执行此操作。 How would I do this on specific columns?
我将如何在特定列上执行此操作? Say columns 3 to 50?
说第 3 到 50 列? Thanks.
谢谢。
ms_10 <- ms_10[, sapply(ms_10, function(col) length(unique(col))) > 440]
You can subset the data first and then apply the same function.您可以先对数据进行子集化,然后再应用相同的 function。
new_data <- ms_10[3:50]
new_data <- new_data[, sapply(new_data, function(col) length(unique(col))) > 440]
If you don't want to create temporary variable ( new_data
).如果您不想创建临时变量 (
new_data
)。
ms_10[3:50][, sapply(ms_10[3:50], function(col) length(unique(col))) > 440]
We can also use dplyr
:我们也可以使用
dplyr
:
library(dplyr)
ms_10 %>%
select(3:50) %>%
select(where(~n_distinct(.) > 440))
We can use我们可以用
Filter(function(x) length(unique(x)) > 440, ms_10[3:50])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.