Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split column in R to several columns based on column value

Tags:

split

dataframe

r

I want to introduce new cols in df based on one column's values.

category col needs to be split on ',' and each split substring needs to go to newly introduced appropriate col.

Minimal example:

df <- data.frame(category=c('1, 2', '1, 3','3', '2, 3'),
                 othercolumn= c("Grealish", "Saka", "Henry", 'Jesus'))

Required df:

finaldf <- data.frame(category=c('1, 2', '1, 3','3', '2, 3'),
                      category1=c('1', '1',NA, NA),
                      category2=c('2',NA,NA, '2'),
                      category3=c(NA,'3','3', '3'),
                 othercolumn= c("Grealish", "Saka", "Henry", 'Jesus'))

category1, category2, and category3 cols will take values based on the category column, otherwise NA.

like image 865
AAA Avatar asked Oct 15 '25 08:10

AAA


1 Answers

In base R you can do:

Using strsplt to split the numbers and convert it to numeric

setName the return in lapply and cbind to data frame

nums <- strsplit(df$category, ",") |> 
  type.convert(as.is = T)

cbind(df[1], do.call(rbind, lapply(nums, \(x){
    sq <- seq(max(unlist(nums)))
    ifelse(sq %in% x , sq, NA ) |> 
      setNames(paste0("category", sq))
  })), df[-1])

  category category1 category2 category3 othercolumn
1     1, 2         1         2        NA    Grealish
2     1, 3         1        NA         3        Saka
3        3        NA        NA         3       Henry
4     2, 3        NA         2         3       Jesus
like image 156
Just James Avatar answered Oct 17 '25 00:10

Just James



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!