将一列分成两列:R 中的 Column1-1st split 和 column2-rest

Split one column into two : Column1-1st split and column2-rest in R

我的数据在单列中,我想将输出拆分为 2 column.I 需要两个输出列。

输入列是....

HOUSEHOLDS-Total households - Female householder- under 18 years  
Total households - Female householder- under 18 years

我想要输出...

第 1 列

HOUSEHOLDS  
Total households

第 2 列

Total households - Female householder- under 18 years  
Female householder- under 18 years

如果这是您的数据集:

df1<- c("HOUSEHOLDS-Total households - Female householder- under 18 years", "Total households - Female householder- under 18 years")

您可以使用:

regmatches(df1, regexpr("-", df1), invert = TRUE)
[[1]]
[1] "HOUSEHOLDS-Total households "       "Female householder- under 18 years"

[[2]]
[1] "Total households "                   " Female householder- under 18 years"

输出是一个列表。

以下是使用 tidyr 中的 separate 的方法。基本上,您在第一次出现“-”时就分开了。由于 extra = "merge".

,其他事件将被忽略
df <- read.table(text="'HOUSEHOLDS-Total households - Female householder- under 18 years'  
                      'Total households - Female householder- under 18 years'",
                       header=FALSE,stringsAsFactors=FALSE)

library(tidyr)
df %>% separate(V1, into = c('Col1', 'Col2'),  sep="-", extra = "merge").
               Col1                                                  Col2
1        HOUSEHOLDS Total households - Female householder- under 18 years
2 Total households                     Female householder- under 18 years