将列拆分成多列R [英] Split column into multiple columns R
本文介绍了将列拆分成多列R的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
I:500-600
I:700-900
II: 250
我想将其拆分为以下3列:
V1 V2 V3
I 500 600
I 700 900
II 200 250
这比我所希望的要好一些。任何帮助将不胜感激。
解决方案
另一个解决方案是从 str_match
stringr
package:
x< - c(I:500 -600,I:700-900,II:200-250)
库(stringr)
as.data.frame(str_match(x,^(。*) 。*) - (。*)$)[, - 1])$ b $ b ## V1 V2 V3
## 1 I 500 600
## 2 I 700 900
## 3 II 200 250
在上述正则表达式中,我们匹配3个子字符串:从开始到:
,从:
到 -
,从 -
到最后。每个匹配的子字符串将在结果对象中构成单独的列。
I have a data frame column that I need to split into 3 separate column. Looks like this:
I:500-600
I:700-900
II:200-250
I'd like to split this into the following 3 columns:
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.
解决方案
Another solution with str_match
from the stringr
package:
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. Each matched substring will constitute a separate column in the resulting object.
这篇关于将列拆分成多列R的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文