[英]Split column into multiple columns R
I have a data frame column that I need to split into 3 separate column. 我有一个数据框列,我需要拆分成3个单独的列。 Looks like this:
看起来像这样:
I:500-600
I:700-900
II:200-250
I'd like to split this into the following 3 columns: 我想将其拆分为以下3列:
V1 V2 V3
I 500 600
I 700 900
II 200 250
This has proved slightly trickier than I had hoped. 事实证明这比我希望的要复杂一些。 Any help would be appreciated.
任何帮助,将不胜感激。
You can use strsplit
with an OR argument splitting using :
or -
this will give you a list which you can process further. 您可以使用
strsplit
和OR参数拆分使用:
或-
这将为您提供一个可以进一步处理的列表。
> test <- c('I:500-600', 'I:700-900', 'II:200-250')
> do.call(rbind.data.frame, strsplit(test, ":|-"))
c..I....I....II.. c..500....700....200.. c..600....900....250..
1 I 500 600
2 I 700 900
3 II 200 250
If names are important 如果名字很重要
> as.data.frame(do.call(rbind, strsplit(test, ":|-")))
V1 V2 V3
1 I 500 600
2 I 700 900
3 II 200 250
Another solution with str_match
from the stringr
package: 与另一种溶液
str_match
从stringr
包:
x <- c("I:500-600", "I:700-900", "II:200-250")
library(stringr)
as.data.frame(str_match(x, "^(.*):(.*)-(.*)$")[,-1])
## V1 V2 V3
## 1 I 500 600
## 2 I 700 900
## 3 II 200 250
In the above regular expression we match 3 substrings: from the beginning to :
, from :
to -
, and from -
to the end. 在上面的正则表达式中,我们匹配3个子串:从开头到
:
,从:
到-
,从-
到结尾。 Each matched substring will constitute a separate column in the resulting object. 每个匹配的子字符串将在结果对象中构成一个单独的列。
Other options include extract
from tidyr
其他选项包括
tidyr
extract
library(tidyr)
extract(df1, V1, into=c('V1','V2', 'V3'),
'([^:]*):([0-9]*)-([0-9]*)', convert=TRUE)
# V1 V2 V3
#1 I 500 600
#2 I 700 900
#3 II 200 250
Or tstrsplit
from data.table
. 或
tstrsplit
从data.table
。
library(data.table)#v1.9.5+
setDT(df1)[, tstrsplit(V1, '[:-]', type.convert=TRUE)]
# V1 V2 V3
#1: I 500 600
#2: I 700 900
#3: II 200 250
NOTE: Both options have arguments to convert the class of the output columns 注意:两个选项都有转换输出列类的参数
df1 <- structure(list(V1 = c("I:500-600", "I:700-900", "II:200-250")),
.Names = "V1", class = "data.frame", row.names = c(NA, -3L))
I would recommend cSplit
from my "splitstackshape" package. 我会从我的“splitstackshape”包中推荐
cSplit
。
The syntax is pretty straightforward: cSplit(yourInputDataFrame, yourSplittingColumn, theDelimiters)
. 语法非常简单:
cSplit(yourInputDataFrame, yourSplittingColumn, theDelimiters)
。
Here's an example on a vector
. 这是一个
vector
的例子。 You'd skip the data.table
part if you already had a data.frame
or a data.table
. 如果您已经有
data.frame
或data.table
则跳过data.table
部分。
library(splitstackshape)
cSplit(data.table(x), "x", ":|-", fixed = FALSE)
# x_1 x_2 x_3
# 1: I 500 600
# 2: I 700 900
# 3: II 200 250
By default, it also runs type.convert
: 默认情况下,它还运行
type.convert
:
str(.Last.value)
# Classes ‘data.table’ and 'data.frame': 3 obs. of 3 variables:
# $ x_1: Factor w/ 2 levels "I","II": 1 1 2
# $ x_2: int 500 700 200
# $ x_3: int 600 900 250
# - attr(*, ".internal.selfref")=<externalptr>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.