【发布时间】:2012-10-18 03:59:19
【问题描述】:
所以我有data.frame
dat = data.frame(x = c('Sir Lancelot the Brave', 'King Arthur',
'The Black Knight', 'The Rabbit'), stringsAsFactors=F)
> dat
x
1 Sir Lancelot the Brave
2 King Arthur
3 The Black Knight
4 The Rabbit
我想把它转换成数据框
> dat2
x 1 2 3 4
1 Sir Lancelot the Brave Sir Lancelot the Brave
2 King Arthur King Arthur
3 The Black Knight The Black Knight
4 The Rabbit The Rabbit
strsplit 以列表形式返回数据
sbt <- strsplit(dat$x, " ")
> sbt
[[1]]
[1] "Sir" "Lancelot" "the" "Brave"
[[2]]
[1] "King" "Arthur"
[[3]]
[1] "The" "Black" "Knight"
[[4]]
[1] "The" "Rabbit"
并且 as.data.table 不会在应该的地方创建 NULL 值,而是重复值
> t(as.data.table(sbt))
[,1] [,2] [,3] [,4]
V1 "Sir" "Lancelot" "the" "Brave"
V2 "King" "Arthur" "King" "Arthur"
V3 "The" "Black" "Knight" "The"
V4 "The" "Rabbit" "The" "Rabbit"
我想我真的很想为 as.data.table(x, repeat=FALSE) 提供一个参数,否则我该如何完成这项工作?
【问题讨论】:
-
您使用的是
data.frames还是data.tables? -
@mnel:只要能完成工作。当我尝试强制 sbt 时,as.data.frame 会出错,所以这就是我尝试使用 as.data.table 的原因。
标签: r data.table strsplit