0

How can I split one column in multiple columns in R using spaces as separators? I tried to find an answer for few hours (even days) but now I count on you guys to help me!

This is how my data set looks like and it's all in one column, I don't really care about the column names as in the end I will only need a few of them for my analysis:

[1]  1000.0    246                                                               
[2]   970.0    491   -3.3   -5.0     88   2.73    200      4  272.2  279.8  272.7
[3]   909.0   1002   -4.7   -6.6     87   2.58    200     12  275.9  283.2  276.3
[4]   900.0   1080   -5.5   -7.5     86   2.43    200     13  275.8  282.8  276.2
[5]   879.0   1264   -6.5   -8.8     84   2.25    200     16  276.7  283.1  277.0
[6]   850.0   1525   -6.5  -12.5     62   1.73    200     20  279.3  284.4  279.6

Also, I tried the separate function and it give me an error telling me that this is not possible for a function class object.

Thanks a lot for your help!

Sanda
  • 1
  • 1
  • Maybe, we can use `read.table(text = as.character(df1$yourcolumn), header = FALSE, fill = TRUE)` – akrun Feb 07 '20 at 23:17

2 Answers2

0

The read.table/read.csv would work if we pass it as a character vector

read.table(text = data_vector, header = FALSE, fill = TRUE)
#    V1   V2   V3    V4 V5   V6  V7 V8    V9   V10   V11
#1 1000  246   NA    NA NA   NA  NA NA    NA    NA    NA
#2  970  491 -3.3  -5.0 88 2.73 200  4 272.2 279.8 272.7
#3  909 1002 -4.7  -6.6 87 2.58 200 12 275.9 283.2 276.3
#4  900 1080 -5.5  -7.5 86 2.43 200 13 275.8 282.8 276.2
#5  879 1264 -6.5  -8.8 84 2.25 200 16 276.7 283.1 277.0
#6  850 1525 -6.5 -12.5 62 1.73 200 20 279.3 284.4 279.6

data

 data_vector <- c("1000.0    246",
                     "970.0    491   -3.3   -5.0     88   2.73    200      4  272.2  279.8  272.7",
                     "909.0   1002   -4.7   -6.6     87   2.58    200     12  275.9  283.2  276.3",
                     "900.0   1080   -5.5   -7.5     86   2.43    200     13  275.8  282.8  276.2",
                     "879.0   1264   -6.5   -8.8     84   2.25    200     16  276.7  283.1  277.0",
                     "850.0   1525   -6.5  -12.5     62   1.73    200     20  279.3  284.4  279.6")
akrun
  • 874,273
  • 37
  • 540
  • 662
0

It's always easier to help if there is minimal reproducible example in the question. The data you show is not easily usable...

MRE:

    data_vector <- c("1000.0    246",
                     "970.0    491   -3.3   -5.0     88   2.73    200      4  272.2  279.8  272.7",
                     "909.0   1002   -4.7   -6.6     87   2.58    200     12  275.9  283.2  276.3",
                     "900.0   1080   -5.5   -7.5     86   2.43    200     13  275.8  282.8  276.2",
                     "879.0   1264   -6.5   -8.8     84   2.25    200     16  276.7  283.1  277.0",
                     "850.0   1525   -6.5  -12.5     62   1.73    200     20  279.3  284.4  279.6")

And here is a solution using gsub and read.csv:

    oo <- read.csv(text=gsub(" +", " ", paste0(data_vector, collapse="\n")), sep=" ", header=FALSE)

Which produces this output:

        V1   V2   V3    V4 V5   V6  V7 V8    V9   V10   V11
    1 1000  246   NA    NA NA   NA  NA NA    NA    NA    NA
    2  970  491 -3.3  -5.0 88 2.73 200  4 272.2 279.8 272.7
    3  909 1002 -4.7  -6.6 87 2.58 200 12 275.9 283.2 276.3
    4  900 1080 -5.5  -7.5 86 2.43 200 13 275.8 282.8 276.2
    5  879 1264 -6.5  -8.8 84 2.25 200 16 276.7 283.1 277.0
    6  850 1525 -6.5 -12.5 62 1.73 200 20 279.3 284.4 279.6
dario
  • 6,415
  • 2
  • 12
  • 26