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

Combining Multiple Columns with Tidyr's Unite by Referencing Similar Column Names

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 的 "unite" 将类别重新组合成单​​列。

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

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

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

是这样的吗?如果您有很多列。

result<-with(new.env(),{
  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 "," "," ")
  x<-ls()
  categories<-unique(sub("(.*)_(.*)", "\1", x))
  df<-setNames(data.frame( lapply(x, function(y) get(y))), x)
  for(nm in categories){
    df<-unite_(df, nm, x[contains(vars = x, match = nm)])
  }
  return(df)
})

Client CommunicationType    Satisfaction       Sex
1 Client1           Email_      Satisfied_      _Male
2 Client2           _Phone   _VerySatisfied Female_NA
3 Client3           _Phone   _VerySatisfied     _Male
4 Client4           Email_      Satisfied_  Female_NA
5 Client5           Email_      Satisfied_      _Male

我们可以使用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