1

I have following data

Data <- data.frame(
X = ("123 234 345 456","222 333 444 555 666" )
)


Data
#        X    
# 123 234 345 456 
# 222 333 444 555 666

A String in one cell, and the length of string is not same in each row

I want the following result

>Result
#    X    Y    Z    A    B
#   123  234  345  456  
#   222  333  444  555  666

one word in one cell

Can anybody help?

Sandy Muspratt
  • 31,719
  • 12
  • 116
  • 122
Dryad
  • 41
  • 5

1 Answers1

4

strsplit is not required here. read.table should work fine. Try:

read.table(text = as.character(Data$X), header=FALSE, fill=TRUE)

You will have to rename the resulting variable names though.

A5C1D2H2I1M1N2O1R2T1
  • 190,393
  • 28
  • 405
  • 485
  • Hello, read.table is work, but the empty cell is filled as NA, how can I change NA as 0? – Dryad Jun 10 '13 at 07:11
  • Assuming the output is stored as `Result`, try `Result[is.na(Result)] <- 0` – A5C1D2H2I1M1N2O1R2T1 Jun 10 '13 at 07:18
  • There is another problem, if the first line is 14 column, and the whole table is fixed 14 column, and longer row will be wrapped. How can I fix this problem? – Dryad Jun 10 '13 at 07:25
  • 2
    @Dryad, you're joking right? You've removed your acceptance of the answer because of how R displays wide tables on your screen? Look into `options` and try to figure it out. – A5C1D2H2I1M1N2O1R2T1 Jun 10 '13 at 07:53
  • I read all options but I don't know which one should be set. Please help me fix this problem, and I will accept your answer again. Thank you! – Dryad Jun 10 '13 at 08:01
  • 1
    @Dryad how hard you read the page? `width: controls the maximum number of columns on a line used in printing vectors, matrices and arrays` – Simon O'Hanlon Jun 10 '13 at 08:14
  • @SimonO101, how can I set the max number of columns in read.table options? – Dryad Jun 10 '13 at 08:47