Search code examples
rmutate

convert column into new column with parentheses around each item in list (for each row) in R


Trying to convert column into new column with parentheses around each item (separated by commas)

Hi there, is it possible to turn this

Column1
a
a, b 
a, b, c

into this

Column1   Column2
a          "a"
a, b       "a", "b"       
a, b, c    "a", "b", "c" 


Solution

  • You did not give sample data for us to work with but this is a sample of how you can get this done.

    Column1<-LETTERS[1:20]  # sample dataset
      
      df1<-data.frame(Column1) # convert to dataframe
      
      updated_df<-df1%>%
        mutate(Column2 = paste0("'",df1$Column1,"'")) #creating column2 with mutate function and using paste0 function to add your desired parenthesis or bracket or whatever you wanna add.
    

    hope this answered your question