colsplit不保留数据帧中的所有列

时间:2017-02-26 17:28:23

标签: r

我已经使用colsplit将列拆分为两列。我的数据框由拆分中的两列组成。我丢失了数据框中的所有其他列。如何拆分一列并将新拆分列添加到当前数据框?

拆分前的数据框

              X0      X1 Period
           <chr>   <dbl>  <int>
1 1973 January   108.289      1
2 1973 February   97.698      2
3 1973 March      97.366      3
4 1973 April      93.084      4
5 1973 May        94.346      5
6 1973 June       97.757      6

应用colsplit

> A <- colsplit(subemission$X0, " ", c("Year", "Month"))
> head(A)
  Year     Month
1 1973 January  
2 1973 February 
3 1973 March    
4 1973 April    
5 1973 May      
6 1973 June     

1 个答案:

答案 0 :(得分:2)

我们可以使用separate中的tidyrremove=FALSE

library(tidyverse)
separate(subemission, XO, into = c("Year", "Month"), remove = FALSE)

cSplit

cSplit(subemission, "XO", " ", drop = FALSE)

base R

read.table选项
cbind(subemission, read.table(text= subemission$XO, 
   header=FALSE, col.names = c("Year", "Month"), stringsAsFactors=FALSE))