问题描述
当使用 .SD
将函数应用于 dt
的子集找到正确的方式来处理我重复的列名称的情况...例如
When using .SD
to apply a function to a subset of dt
's columns I can't seem to find the correct way to handle the situation where I have duplicated column names... e.g.
# Make some data
set.seed(123)
dt <- data.table( matrix( sample(6,16,repl=T) , 4 ) )
setnames(dt , rep( letters[1:2] , 2 ) )
# a b a b
#1: 2 6 4 5
#2: 5 1 3 4
#3: 3 4 6 1
#4: 6 6 3 6
# Use .SDcols to multiply both column 'a' specifying them by numeric position
dt[ , lapply( .SD , `*` , 2 ) , .SDcols = which( names(dt) %in% "a" ) ]
# a a
#1: 4 4
#2: 10 10
#3: 6 6
#4: 12 12
我不能让它与 .SDcols
是列名的字符向量,所以我尝试数字位置( which(names(dt)%in%a)
给出一个向量 [1] 1 3
),但它似乎只是乘以第一个 a
列。我做错了什么?
I couldn't get it to work with when .SDcols
was a character vector of column names so I tried numeric positions (which( names(dt) %in% "a" )
gives a vector [1] 1 3
) but it also seems to just multiply the first a
column only. Am I doing something wrong?
这些也返回与上面相同的结果...
These also returned the same result as above...
dt[ , lapply( .SD ,function(x) x*2 ) , .SDcols = which( names(dt) %in% "a" ) ]
dt[ , lapply( .SD ,function(x) x*2 ) , .SDcols = c(1,3) ]
packageVersion("data.table")
#[1] ‘1.8.11’
推荐答案
现在可以按照的要求。从新闻:
This now works as intended in the current development version 1.9.3. From NEWS:
基本上,如果你这样做:
Basically, if you do:
dt[, lapply(.SD, `*`, 2), .SDcols=c("a", "a")]
# a a
# 1: 4 4
# 2: 10 10
# 3: 6 6
# 4: 12 12
但是,如果你明确指定(如你在你的Q):
But if you clearly specify (as you do in your Q):
dt[, lapply(.SD, `*`, 2), .SDcols=which( names(dt) %in% "a" )]
# a a
# 1: 4 8
# 2: 10 6
# 3: 6 12
# 4: 12 6
这篇关于当使用.SDcols时,data.table可以处理相同的列名吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!