0

I would like to convert the following object to a data frame with 7 columns. Some of the rows in the example contains 6 strings (separated by a space), and some 7 (row #3). For those that contains 6, we should just put NA in the 7th column. I have tried unlist and similar, but it did not work properly.

   structure(list(`my_list` = c("hsa_miR_942_5p -0.01020 0.98986 0.04543 -0.224 0.822", 
    "hsa_miR_95_3p -0.05272 0.94864 0.04607 -1.145 0.252", "hsa_miR_96_5p -0.14028 0.86912 0.04114 -3.410 0.00065 ***", 
    "hsa_miR_98_5p -0.06664 0.93554 0.05587 -1.193 0.233", "hsa_miR_99a_5p -0.02230 0.97795 0.04625 -0.482 0.630", 
    "hsa_miR_99b_5p 0.01528 1.01539 0.04502 0.339 0.734")), row.names = 344:349, class = "data.frame")
user2300940
  • 2,355
  • 1
  • 22
  • 35
  • 1
    It seems you read the data incorrectly. You could/should fix the issue while reading itself (by using appropriate separator) . if you want to do it based on the data that you have now use `splitstackshape::cSplit(df, 'my_list', sep = ' ')` which splits the data into 7 columns. – Ronak Shah Nov 18 '20 at 07:51
  • Thanks, it worked. But what do you mean by read incorrectly? – user2300940 Nov 18 '20 at 07:55
  • I did the following to create this example data, maybe the error is here: as.data.frame(gsub("\\s+", " ", df) ) – user2300940 Nov 18 '20 at 07:56
  • How did you get `df` ? Did you read it from csv file? How did you read it? – Ronak Shah Nov 18 '20 at 07:57
  • From a coxph object: df <- as.matrix(as.list(coxph)[grep("hsa", as.list(coxph))]) – user2300940 Nov 18 '20 at 08:01

0 Answers0