R 数字向量和数据表的索引
将以下向量与每个数字的索引一起放入data.table的最佳方法是什么R 数字向量和数据表的索引,r,data.table,R,Data.table,将以下向量与每个数字的索引一起放入data.table的最佳方法是什么 nVector <- c("20 37", "38 23", "39 48", "45 76", "65 44", "86 95 80") nVector为什么不干脆: data.table(v1 = nVector)[, index := .I][, list(unlist(strsplit(v1, " "))), by = index] ## index V1 ## 1: 1 20 ## 2:
nVector <- c("20 37", "38 23", "39 48", "45 76", "65 44", "86 95 80")
nVector为什么不干脆:
data.table(v1 = nVector)[, index := .I][, list(unlist(strsplit(v1, " "))), by = index]
## index V1
## 1: 1 20
## 2: 1 37
## 3: 2 38
## 4: 2 23
## 5: 3 39
## 6: 3 48
## 7: 4 45
## 8: 4 76
## 9: 5 65
## 10: 5 44
## 11: 6 86
## 12: 6 95
## 13: 6 80
或者,您可以创建如下函数(使用函数更方便重用——如果只是一次性问题,则不需要):
更新
由于性能是考虑数据大小的一个问题,您可能希望使用fun
方法,手动创建“data.table”
以下是更大版本的向量的一些计时:
NVector <- rep(nVector, 10000)
length(NVector)
# [1] 60000
f1 <- function(invec) {
data.table(v1 = invec)[, index := .I][
, list(unlist(strsplit(v1, " ", TRUE))), by = index]
}
f2 <- function(invec) {
cSplit(data.table(v1 = invec)[, index := .I],
"v1", sep = " ", direction = "long")
}
library(microbenchmark)
microbenchmark(fun(NVector), f1(NVector), f2(NVector), times = 50)
# Unit: milliseconds
# expr min lq mean median uq max neval
# fun(NVector) 13.26559 13.70738 15.89918 14.12573 15.11083 50.84675 50
# f1(NVector) 196.95570 207.60004 223.74729 212.49649 224.78725 378.51007 50
# f2(NVector) 167.38512 176.16370 196.28389 183.96098 202.00187 412.71760 50
以下是新功能:
## like `fun`, but using `stri_split_fixed`
fun_stringi <- function(invec) {
x <- stri_split_fixed(invec, " ")
data.table(index = rep(seq_along(x), lengths(x)), V1 = unlist(x, use.names = FALSE))
}
## A base R alternative
f3 <- function(invec) stack(setNames(strsplit(invec, " ", TRUE), seq_along(invec)))
## A tidyverse approach
f4 <- function(invec) {
data_frame(ind = seq_along(invec),
val = stri_split_fixed(invec, " ")) %>%
unnest()
}
##喜欢'fun',但使用'stri#u split#u固定`
fun_stringi同意Ananda的答案很好,但这里有一个更为粗野的forcish方法,使用stringr
包和data.frames进行未来的推断
foo = data.frame(V1 = matrix(nVector, ncol = 1))
foo = data.frame(str_split_fixed(foo$V1, " ", 3))
bar = cbind(id = rownames(foo), foo)
bar = melt(bar, id.vars = "id")
bar = bar[order(bar$id),]
bar = bar[bar$value != "",-2]
## id value
## 1 20
## 1 37
## 2 38
## 2 23
## 3 39
## 3 48
## 4 45
## 4 76
## 5 65
## 5 44
## 6 86
## 6 95
## 6 80
@Nancy我想提到的是,为了解释预分配的listAnanda的data.table
解决方案非常好,但仅供将来参考,我想指出,对于这样的任务,无论您是否使用data.table
,都不需要循环。例如,要创建索引向量,您可以执行idx=rep(1:length(nVector)、sapply(strsplit(nVector,split=”“),length))
。这充分利用了R中的许多函数是矢量化的这一事实,这意味着它们只需对函数进行一次调用即可对向量的每个元素进行操作。@eipi10。。。要改进您的建议,请使用length
而不是sapply(…,length)
。快多了……谢谢@AnandaMahto。很高兴知道长度
。啊,但你的方式仍然会在code高尔夫中获胜。或者:库(splitstackshape);最后,当提出问题的人给出了一个合理的方法时,我们不能总是忽略它,但我们期望更有效的代码来解决更大的问题。例如,在这里的回答中,您遇到了一个问题,您硬编码了要与stri\u split\u fixed
一起使用的分割数。当他们表示他们的平均项目长度在160个项目范围内时,可能不是一个好主意;-)不管怎样,继续给我时间。我们在这里可以直言不讳,但当我们相互了解时,也会非常友好……@Ananda一点也不粗鲁。我很高兴了解并感谢您的反馈!我也倾向于从我作为一个用户想要什么的角度来处理这样的问题,如果我找到了答案。有时“最佳”答案更难理解,用不同的方式来看待它会有所帮助。。。也许是一些更有经验的人使用的方法。不值得接受的答案,但不一定是无用的。
NVector <- rep(nVector, 10000)
length(NVector)
# [1] 60000
f1 <- function(invec) {
data.table(v1 = invec)[, index := .I][
, list(unlist(strsplit(v1, " ", TRUE))), by = index]
}
f2 <- function(invec) {
cSplit(data.table(v1 = invec)[, index := .I],
"v1", sep = " ", direction = "long")
}
library(microbenchmark)
microbenchmark(fun(NVector), f1(NVector), f2(NVector), times = 50)
# Unit: milliseconds
# expr min lq mean median uq max neval
# fun(NVector) 13.26559 13.70738 15.89918 14.12573 15.11083 50.84675 50
# f1(NVector) 196.95570 207.60004 223.74729 212.49649 224.78725 378.51007 50
# f2(NVector) 167.38512 176.16370 196.28389 183.96098 202.00187 412.71760 50
library(stringi)
set.seed(2)
NVec2 <- vapply(sample(20, 60000, TRUE),
function(x) paste(stri_rand_strings(x, 5, "[0-9]"), collapse = " "),
character(1L))
length(NVec2)
# [1] 60000
## like `fun`, but using `stri_split_fixed`
fun_stringi <- function(invec) {
x <- stri_split_fixed(invec, " ")
data.table(index = rep(seq_along(x), lengths(x)), V1 = unlist(x, use.names = FALSE))
}
## A base R alternative
f3 <- function(invec) stack(setNames(strsplit(invec, " ", TRUE), seq_along(invec)))
## A tidyverse approach
f4 <- function(invec) {
data_frame(ind = seq_along(invec),
val = stri_split_fixed(invec, " ")) %>%
unnest()
}
library(microbenchmark)
res <- microbenchmark(base = fun(NVec2), stringi = fun_stringi(NVec2),
data_table = f1(NVec2), splitstackshape = f2(NVec2),
base_alt = f3(NVec2), tidyverse = f4(NVec2), times = 50)
res
# Unit: milliseconds
# expr min lq mean median uq max neval
# base 162.6149 174.7311 204.0177 187.3446 213.7267 443.8357 50
# stringi 146.8655 157.6717 187.1125 168.5383 192.1952 394.1169 50
# data_table 360.0788 382.9118 427.2276 396.0421 418.1821 598.3754 50
# splitstackshape 542.8882 578.6317 619.9677 598.5113 626.5734 901.9400 50
# base_alt 259.2847 293.7944 325.6021 310.7322 339.1613 492.4644 50
# tidyverse 500.1571 519.4765 545.4757 534.1167 549.4756 713.3711 50
library(stringi)
set.seed(2)
NVec3 <- vapply(sample(100:200, 125000, TRUE),
function(x) paste(stri_rand_strings(x, 5, "[0-9]"), collapse = " "),
character(1L))
system.time({out <- f2(NVec3)})
# user system elapsed
# 20.89 0.03 20.94
## Similar to your actual data
length(NVec3)
# [1] 125000
nrow(out)
# [1] 18767938
res <- microbenchmark(base = fun(NVec3), stringi = fun_stringi(NVec3),
data_table = f1(NVec3), base_alt = f3(NVec3),
tidyverse = f4(NVec3), times = 20)
res
## Unit: seconds
## expr min lq mean median uq max neval
## base 4.967281 5.606208 5.983120 5.978414 6.345823 7.189997 20
## stringi 4.888080 5.292926 5.811898 5.728464 6.091029 7.923210 20
## data_table 5.625772 5.861431 6.244174 6.092079 6.420082 7.698534 20
## base_alt 4.635496 5.015382 5.564661 5.486531 6.090838 7.034357 20
## tidyverse 5.634781 6.186927 6.717203 6.613003 7.198013 8.154297 20
autoplot(res, log = FALSE)
foo = data.frame(V1 = matrix(nVector, ncol = 1))
foo = data.frame(str_split_fixed(foo$V1, " ", 3))
bar = cbind(id = rownames(foo), foo)
bar = melt(bar, id.vars = "id")
bar = bar[order(bar$id),]
bar = bar[bar$value != "",-2]
## id value
## 1 20
## 1 37
## 2 38
## 2 23
## 3 39
## 3 48
## 4 45
## 4 76
## 5 65
## 5 44
## 6 86
## 6 95
## 6 80