Home > Back-end >  Combine 3 columns to one
Combine 3 columns to one

Time:04-24

I want to combine multiple columns to one column in a data frame. I want a column that only shows last name and first name. I have a large data that looks similar to the following:

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
>

This is what I have tried so far:

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

The result is not what I'm looking for.

Any suggestion on how to fix this so that the new column will only show last name, first name?

CodePudding user response:

Is this what you want?

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

CodePudding user response:

Great solution up there. If you are looking for something more simple you can use the unite() function from the package tidyr. Here is the syntax:

install.packages("tidyr") #or get everything with "tidyverse"
library(tidyr)
unite(Alldata, col="Name&surname", Name_a, Last_n, sep="#anyseparatoryouwant") 

This is straightforward right? the first argument is your dataframe: Alldata, the col= wants the name of the future column, pick anything; then you give the names of the n columns you want to join: Name_a and Last_n in this case. Last you can add a separator with the sep= argument, you can event put a space just by entering:

sep=" "

Check the unite() arguments with

?unite

Hope this made it more simple!

  • Related