本文介绍了动态分配data.table tstrsplit中的拆分数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在 data.table v.1.9.6
中,您可以按以下方式在列中拆分变量:
In data.table v.1.9.6
you can split a variable in columns like so:
library(data.table)
DT = data.table(x=c("A/B", "A", "B"), y=1:3)
DT[, c("c1", "c2") := tstrsplit(x, "/", fixed=TRUE)][]
并非总是预先知道所需的分割数[以上:2]。
已知拆分数时如何生成所需的变量名?
The number of required splits [above: 2] is not always known in advance.How can I generate the required variable names when the number of splits is known?
n = 2 # desired number of splits
# naive attempt to build required string
m = paste0("'", "myvar", 1:n, "'", collapse = ",")
m = paste0("c(", m, ")" )
# [1] "c('myvar1','myvar2','myvar3')"
DT[, m := tstrsplit(x, "/", fixed=TRUE)][] # doesn't work
推荐答案
两个方法。强烈建议第一个:
Two methods. The first is strongly suggested:
#one
n=2
DT[, paste0("myvar", 1:n) := tstrsplit(x, "/", fixed=T)][]
# x y myvar1 myvar2
#1: A/B 1 A B
#2: A 2 A NA
#3: B 3 B NA
#two
DT[, eval(parse(text=m)) := tstrsplit(x, "/", fixed=TRUE)][]
# x y myvar1 myvar2
#1: A/B 1 A B
#2: A 2 A NA
#3: B 3 B NA
额外
如果您事先不知道拆分的数量,则:
If you do not know the amount of splits beforehand:
splits <- max(lengths(strsplit(DT$x, "/")))
DT[, paste0("myvar", 1:splits) := tstrsplit(x, "/", fixed=T)][]
这篇关于动态分配data.table tstrsplit中的拆分数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!