我已经使用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
答案 0 :(得分:2)
我们可以使用separate
中的tidyr
和remove=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))