0

I have a dataframe that is constituted by 5 factor columns. One of these columns (D) contains 200 values (on each row) separated by a comma delimiter ','. What I am trying to accomplish is to split this column (D) into 200 columns in the same dataframe if possible.

data= data.frame(A = c('12223','168723','885441','99855'), 
                 B=c('855547','98557','699854','345871'), 
                 C=c('US', 'US', 'UK', 'IT'), 
                 D=c('0.54,0.589,0.55,0.69,0.88,0.98'
                     ,'0.36,0.38,0.66,0.84,0.22,0.33'
                     ,'0.67,0.89,0.94,0.85,0.78,0.98'
                     ,'0.14,0.12,0.13,0.11,0.17,0.19'), 
                 E=c('1','0','1','1'))

I've tried to use the separate function from tidyr but I get an error:

Error in Math.factor(var) : ‘abs’ not meaningful for factors

data %>% separate(data$D, into = paste("V", 1:6, sep = "_"), sep=',')
Jespar
  • 1,017
  • 5
  • 16
  • 29
  • 1
    Just remove the `$` from `separate`, i.e. `data %>% separate(D, into = paste("V", 1:6, sep = "_"), sep=',')` – Sotos Oct 06 '17 at 13:29
  • 1
    In base R, do `cbind(dat[c(1,2,3,5)], do.call(rbind, lapply(strsplit(as.character(dat[[4]]), ","), as.numeric)))` and then rename variables. – lmo Oct 06 '17 at 13:35
  • I saw that post Aramis7d but because, as Sotos stated, I used `data$D` instead of `D` in the `seperate` that's why I couldn't find a solution. Sotos, if you please could you post it as an answer? Thanks – Jespar Oct 06 '17 at 13:36

0 Answers0