【发布时间】:2011-05-20 00:34:12
【问题描述】:
我想获取表单的数据
before = data.frame(attr = c(1,30,4,6), type=c('foo_and_bar','foo_and_bar_2'))
attr type
1 1 foo_and_bar
2 30 foo_and_bar_2
3 4 foo_and_bar
4 6 foo_and_bar_2
并在上面的“type”列上使用split() 来获得类似这样的内容:
attr type_1 type_2
1 1 foo bar
2 30 foo bar_2
3 4 foo bar
4 6 foo bar_2
我想出了一些令人难以置信的复杂问题,其中涉及某种形式的 apply 有效,但后来我放错了地方。这似乎太复杂了,不是最好的方法。我可以使用strsplit,如下所示,但不清楚如何将其恢复到数据框中的 2 列中。
> strsplit(as.character(before$type),'_and_')
[[1]]
[1] "foo" "bar"
[[2]]
[1] "foo" "bar_2"
[[3]]
[1] "foo" "bar"
[[4]]
[1] "foo" "bar_2"
感谢您的任何指点。我还没有完全了解 R 列表。
【问题讨论】:
标签: r string dataframe split r-faq