我使用colsplit将一列分成两部分。我的数据是由拆分的两列组成的。我失去了数据文件中的所有其他列。如何拆分一个列,并将新的拆分列添加到当前的dataframe?
分裂前的数据
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 发布于 2017-02-26 17:32:51
我们可以将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))https://stackoverflow.com/questions/42471767
复制相似问题