-1

I have a dataset with 10 columns and 100,000 rows. I have a column (let's name it "column_A") that contains text, separated by commas: "motor, student, type_C" etc.

I want to split this column and make each element as the name of a column: So it will add the columns "motor", "student", "type_C" to my dataset in order to fill these new columns with other values.

Actually I have splitted the character, but I don't know how to make these as new columns

strsplit(as.character(trimws(data$column_A)),",")

thanks for your help!

Awans
  • 231
  • 1
  • 8
  • 1
    Please provide reproducible data. – s_baldur Oct 20 '20 at 09:58
  • Try these solutions https://stackoverflow.com/questions/4350440/split-data-frame-string-column-into-multiple-columns and https://stackoverflow.com/questions/7069076/split-column-at-delimiter-in-data-frame – Ronak Shah Oct 20 '20 at 10:08

2 Answers2

0

Does this work:

> dt <- data.frame(col_A = c("motor, student, type_C", "motor, student, type_B","motor, student, type_A"), stringsAsFactors = F)
> dt
                   col_A
1 motor, student, type_C
2 motor, student, type_B
3 motor, student, type_A
> dt %>% extract(col = col_A, into = c('C1','C2','C3'), regex = '(.*),\\s(.*),\\s(.*)')
     C1      C2     C3
1 motor student type_C
2 motor student type_B
3 motor student type_A
Karthik S
  • 11,348
  • 2
  • 11
  • 25
0

You can try this:

library("tidyr")
data %>%
  separate(column_A, c("col1", "col2", "col3"), ",")

or with the stringr package:

library("stringr")    
str_split_fixed(data$column_A, ",", 3)

But personally I prefer option one.

Let me know if it worked.

Elias
  • 726
  • 8
  • 20