gpt4 book ai didi

r - 通过引用相似的列名称,使用 Tidyr 的 Unite 组合多个列

转载 作者:行者123 更新时间:2023-12-01 11:21:14 25 4
gpt4 key购买 nike

library(tidyr)
library(dplyr)
library(tidyverse)

下面是一个简单数据框的代码。我有一些乱七八糟的数据,这些数据是用散布在不同列中的列因子类别导出的。

Client<-c("Client1","Client2","Client3","Client4","Client5")
Sex_M<-c("Male","NA","Male","NA","Male")
Sex_F<-c(" ","Female"," ","Female"," ")
Satisfaction_Satisfied<-c("Satisfied"," "," ","Satisfied","Satisfied")
Satisfaction_VerySatisfied<-c(" ","VerySatisfied","VerySatisfied"," "," ")
CommunicationType_Email<-c("Email"," "," ","Email","Email")
CommunicationType_Phone<-c(" ","Phone ","Phone "," "," ")
DF<-tibble(Client,Sex_M,Sex_F,Satisfaction_Satisfied,Satisfaction_VerySatisfied,CommunicationType_Email,CommunicationType_Phone)

我想使用 tidyr 的“联合”将类别重新组合成单​​个列。

DF<-DF%>%unite(Sat,Satisfaction_Satisfied,Satisfaction_VerySatisfied,sep=" ")%>%
unite(Sex,Sex_M,Sex_F,sep=" ")

但是,我必须写多个“联合”行,我觉得这违反了三次规则,所以必须有一种方法可以使这更容易,特别是因为我的真实数据包含几十个需要合并的列。有没有一种方法可以使用一次“联合”,但以某种方式引用匹配的列名,以便所有相似的列名(例如,“Sex_M”和“Sex_F”包含“Sex”,“CommunicationType_Email”包含“CommunicationType”和“CommunicationType_Phone”)与上述公式相结合?

我也在考虑一个允许我输入列名的功能,但这对我来说太难了,因为它涉及复杂的标准评估。

最佳答案

我们可以使用unite

library(tidyverse)
DF %>%
unite(Sat, matches("^Sat"))

对于多种情况,也许

gather(DF, Var, Val, -Client, na.rm = TRUE) %>%
separate(Var, into = c("Var1", "Var2")) %>%
group_by(Client, Var1) %>%
summarise(Val = paste(Val[!(is.na(Val)|Val=="")], collapse="_")) %>%
spread(Var1, Val)
# Client CommunicationType Satisfaction Sex
#* <chr> <chr> <chr> <chr>
#1 Client1 Email Satisfied Male
#2 Client2 Phone VerySatisfied Female
#3 Client3 Phone VerySatisfied Male
#4 Client4 Email Satisfied Female
#5 Client5 Email Satisfied Male

关于r - 通过引用相似的列名称,使用 Tidyr 的 Unite 组合多个列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42661979/

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