将 3 列合并为一列
Combine 3 columns to one
我想将数据框中的多列合并为一列。我想要一个只显示姓氏和名字的列。
我有一个类似于以下内容的大数据:
Name_a<-c("","Steven", "Sara", "Eva", "(unknown)", "Joey", "", "Adam","(unknown)")
Last_n<-c("", "Lohan","","","Bright","Shane","Carter","","Graham")
person_n<-c("Shawn, Paris","(unknown", "Giselle, Sara","Dwayne, Eva","Brigth, Blue", "","Shane, Carter","Cardi, Adam","Graham, Mel" )
Alldata<-data.frame(Name_a,Last_n,person_n)
> Alldata
Name_a Last_n person_n
1 Shawn, Paris
2 Steven Lohan (unknown
3 Sara Giselle, Sara
4 Eva Dwayne, Eva
5 (unknown) Bright Brigth, Blue
6 Joey Shane
7 Carter Shane, Carter
8 Adam Cardi, Adam
9 (unknown) Graham Graham, Mel
>
这是我目前尝试过的方法:
Alldata<-mutate(Alldata,x=paste(Alldata$Name_a, Alldata$Last_n,Alldata$person_n))
Alldata
Name_a Last_n person_n x
1 Shawn, Paris Shawn, Paris
2 Steven Lohan (unknown Steven Lohan (unknown
3 Sara Giselle, Sara Sara Giselle, Sara
4 Eva Dwayne, Eva Eva Dwayne, Eva
5 (unknown) Bright Brigth, Blue (unknown) Bright Brigth, Blue
6 Joey Shane Joey Shane
7 Carter Shane, Carter Carter Shane, Carter
8 Adam Cardi, Adam Adam Cardi, Adam
9 (unknown) Graham Graham, Mel (unknown) Graham Graham, Mel
结果不是我想要的。
关于如何解决此问题以便新列仅显示姓氏和名字的任何建议?
这是你想要的吗?
library(dplyr)
library(stringr)
Alldata %>%
mutate(x = case_when(
((str_detect(person_n, "unknown")) | (nchar(person_n) == 0)) ~ str_c(Last_n, Name_a, sep = ", "),
TRUE ~ person_n
))
x
<chr>
1 Shawn, Paris
2 Lohan, Steven
3 Giselle, Sara
4 Dwayne, Eva
5 Brigth, Blue
6 Shane, Joey
7 Shane, Carter
8 Cardi, Adam
9 Graham, Mel
上面的解决方案很棒。如果您正在寻找更简单的东西,您可以使用包 tidyr 中的 unite() 函数。语法如下:
install.packages("tidyr") #or get everything with "tidyverse"
library(tidyr)
unite(Alldata, col="Name&surname", Name_a, Last_n, sep="#anyseparatoryouwant")
这很简单吧?第一个参数是你的数据框:Alldata,
col= 想要未来列的名称,选择任何东西;然后给出要加入的 n 列的名称:在本例中为 Name_a 和 Last_n。最后你可以添加一个带有 sep= 参数的分隔符,你可以输入 space 事件:
sep=" "
用
检查 unite() 参数
?unite
希望这能让事情变得更简单!
我想将数据框中的多列合并为一列。我想要一个只显示姓氏和名字的列。 我有一个类似于以下内容的大数据:
Name_a<-c("","Steven", "Sara", "Eva", "(unknown)", "Joey", "", "Adam","(unknown)")
Last_n<-c("", "Lohan","","","Bright","Shane","Carter","","Graham")
person_n<-c("Shawn, Paris","(unknown", "Giselle, Sara","Dwayne, Eva","Brigth, Blue", "","Shane, Carter","Cardi, Adam","Graham, Mel" )
Alldata<-data.frame(Name_a,Last_n,person_n)
> Alldata
Name_a Last_n person_n
1 Shawn, Paris
2 Steven Lohan (unknown
3 Sara Giselle, Sara
4 Eva Dwayne, Eva
5 (unknown) Bright Brigth, Blue
6 Joey Shane
7 Carter Shane, Carter
8 Adam Cardi, Adam
9 (unknown) Graham Graham, Mel
>
这是我目前尝试过的方法:
Alldata<-mutate(Alldata,x=paste(Alldata$Name_a, Alldata$Last_n,Alldata$person_n))
Alldata
Name_a Last_n person_n x
1 Shawn, Paris Shawn, Paris
2 Steven Lohan (unknown Steven Lohan (unknown
3 Sara Giselle, Sara Sara Giselle, Sara
4 Eva Dwayne, Eva Eva Dwayne, Eva
5 (unknown) Bright Brigth, Blue (unknown) Bright Brigth, Blue
6 Joey Shane Joey Shane
7 Carter Shane, Carter Carter Shane, Carter
8 Adam Cardi, Adam Adam Cardi, Adam
9 (unknown) Graham Graham, Mel (unknown) Graham Graham, Mel
结果不是我想要的。
关于如何解决此问题以便新列仅显示姓氏和名字的任何建议?
这是你想要的吗?
library(dplyr)
library(stringr)
Alldata %>%
mutate(x = case_when(
((str_detect(person_n, "unknown")) | (nchar(person_n) == 0)) ~ str_c(Last_n, Name_a, sep = ", "),
TRUE ~ person_n
))
x
<chr>
1 Shawn, Paris
2 Lohan, Steven
3 Giselle, Sara
4 Dwayne, Eva
5 Brigth, Blue
6 Shane, Joey
7 Shane, Carter
8 Cardi, Adam
9 Graham, Mel
上面的解决方案很棒。如果您正在寻找更简单的东西,您可以使用包 tidyr 中的 unite() 函数。语法如下:
install.packages("tidyr") #or get everything with "tidyverse"
library(tidyr)
unite(Alldata, col="Name&surname", Name_a, Last_n, sep="#anyseparatoryouwant")
这很简单吧?第一个参数是你的数据框:Alldata, col= 想要未来列的名称,选择任何东西;然后给出要加入的 n 列的名称:在本例中为 Name_a 和 Last_n。最后你可以添加一个带有 sep= 参数的分隔符,你可以输入 space 事件:
sep=" "
用
检查 unite() 参数?unite
希望这能让事情变得更简单!