ではdplyr
、を使用n_distinct
して一意の値をカウントselect_if
し、列を選択できます
library(dplyr)
df1 %>% select(where(~n_distinct(.) > 1))
#For dplyr < 1.0.0
#df1 %>% select_if(~n_distinct(.) > 1)
# Item_Name D_1 D_3
#1 test1 1 11
#2 test2 0 3
#3 test3 1 1
'sとpurrr
'sで同じロジックを使用できますkeep
discard
purrr::keep(df1, ~n_distinct(.) > 1)
purrr::discard(df1, ~n_distinct(.) == 1)
data.table
それを行うその方法とは別に、
library(data.table)
setDT(df1)
df1[, lapply(df1, uniqueN) > 1, with = FALSE]
またはおそらくこれはより賢い/より良いです
df1[, .SD, .SDcols=lapply(df1, uniqueN) > 1]
上記のすべてのアプローチでは、数値列のみをサブセット化した後にn_distinct
/uniqueN
を置換var
または機能させることができます。sd
例えば、
df1[-1] %>% select_if(~sd(.) != 0)