I have a function that removes columns without a given number of unique values. This command does it for the whole data frame. How would I do this on specific columns? Say columns 3 to 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.
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
).
ms_10[3:50][, sapply(ms_10[3:50], function(col) length(unique(col))) > 440]
We can also use 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])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.