[英]R data.table, namespace for user defined function in J
I have a data table that looks like below. 我有一个数据表,如下所示。 I would like to calculate the correlation of return against each signal, for every market. 我想针对每个市场计算每种信号的回报率相关性。
dt = data.table(mkt = rep(letters[1:3], each = 3), rtn = rnorm(9), signal1=rnorm(9), signal2=rnorm(9), signal3 = rnorm(9))
mkt rtn signal1 signal2 signal3
1: a 0.2488643 0.4110516 -0.04861252 -1.3599824
2: a 1.3387256 -0.4418436 -0.17055841 -1.2161698
3: a -1.4058236 -1.2624645 -0.24315048 -1.2722546
4: b 1.7056606 0.2618591 2.60779232 0.7786226
5: b 0.7913587 -1.0596116 0.31152541 1.7336651
6: b -1.8690651 0.1942825 0.95430075 -0.7030462
7: c -0.4937575 -1.8645226 -0.32312077 -1.7138482
8: c -0.7153342 -0.5142624 -0.43817789 -1.3637261
9: c 0.3766730 -0.0954339 0.71159756 -1.2118075
dt[, lapply(.SD, function(x) cor(x, rtn, use = 'c')), .SDcols = 3:5, by = mkt]
Error in is.data.frame(y) : object 'rtn' not found
How can I make the anonymous function in J aware of the rtn column? 如何使J中的匿名函数知道rtn列?
I think one way would be to include it in the .SDcols
so that the anonymous function will be able to find rtn
and then probably remove the rtn
column (because it will only have 1 as a value since it will be the correlation with itself): 我认为一种方法是将其包含在.SDcols
以便匿名函数能够找到rtn
,然后可能会删除rtn
列(因为它将只有1作为值,因为它将是与自身的关联) :
dt[, lapply(.SD, function(x) cor(x, rtn, use = 'c')), .SDcols = c(2, 3:5), by = mkt]
mkt rtn signal1 signal2 signal3
1: a 1 0.6759421 -0.5037837 0.8605805
2: b 1 -0.8494135 0.6720274 0.7832928
3: c 1 -0.9425291 0.5683629 -0.9976231
And then you could do: 然后您可以执行以下操作:
dt2 <- dt[, lapply(.SD, function(x) cor(x, rtn, use = 'c')), .SDcols = c(2, 3:5), by = mkt]
dt2[, rtn := NULL]
dt2
# mkt signal1 signal2 signal3
#1: a 0.6759421 -0.5037837 0.8605805
#2: b -0.8494135 0.6720274 0.7832928
#3: c -0.9425291 0.5683629 -0.9976231
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.