[英]R: update column-name range in data.table
Is there an elegant way to update a range of columns selected by names from a data.table? 有没有一种优雅的方法来更新通过名称从data.table中选择的列的范围?
For example, let's say we have 例如,假设我们有
dt <- data.table(A = LETTERS[1:5], B = LETTERS[6:10], C = LETTERS[11:15])
I would like to add quotes to all columns from A to B using the function(x) paste0('"', x, '"')
. 我想使用function(x) paste0('"', x, '"')
将引号添加到从A到B的所有列中。
I tried: 我试过了:
dt[, .SD := lapply(.SD, function(x) paste0('"', x, '"')), .SDcols=A:B]
Without result. 没有结果。
We can use the names
or even have something like c("A", "B")
我们可以使用names
,甚至可以使用类似c("A", "B")
f1 <- function(dat, v1) Reduce(seq, which(colnames(dat) %in% v1))
i1 <- f1(dt, c('A', 'B'))
dt[, (i1) := lapply(.SD, function(x) paste0('"', x, '"')), .SDcols=A:B]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.