简体   繁体   English

根据r中的空格将一列拆分为多列

[英]Split one column into multiple based on spaces in r

How can I split one column in multiple columns in R using spaces as separators?如何使用空格作为分隔符将 R 中的一列拆分为多列? 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!非常感谢你的帮助!

The read.table/read.csv would work if we pass it as a character vector如果我们将其作为character向量传递,则read.table/read.csv将起作用

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")

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 :这是使用gsubread.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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM