gpt4 book ai didi

r - 通过匹配给定因子水平的变量/列选择数据框中的变量/列

转载 作者:行者123 更新时间:2023-12-05 02:58:08 25 4
gpt4 key购买 nike

是否可以通过匹配特定因子水平的那些来选择数据框中的变量,根据它们的因子水平(使用或未使用)选择列?我可以summarise by levels或可能的子集 by rows ,但我想知道是否可以从数据框中选择列,或者至少列出具有特定因子水平的变量/列。

library(dplyr)
height <- c(132,151,162,139,166,147,122)
weight <- c(48,49,66,53,67,52,40)
gender <- c("male","male","female","female","male","female","male")
gender2 <- c("female","male","male","male","male","female","male")
genderx <- c("xfemale","malex","malex","male","male","xfemale","xfemale")


df <- data.frame(height,weight,gender, gender2, genderx) %>%
rowid_to_column(., "ID")

喜欢(或不喜欢)的东西

%>% select (vars(levels ==(c("male", "female")))

最佳答案

我们可以使用select_if

library(dplyr)
df %>%
select_if(~ is.factor(.) && all(c("male", "female") %in% levels(.)))

也可以是任意

df %>% 
select_if(~ is.factor(.) && any(c("male", "female") %in% levels(.)))

关于r - 通过匹配给定因子水平的变量/列选择数据框中的变量/列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59326623/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com