The R I have a data table with two dimensions red, and greenand would calculate their total correlation.
library(data.table)
DT <- data.table(red = c(1, 2, 3, 4, 5, 6.5, 7.6, 8.7),
green = c(2, 4, 6, 8, 10, 12, 14, 16),
id = 1:8)
How can I get the following output in a single data.table command?
...
> DT[1:5, cor(red, green)]
[1] 1 # should go into row 5
> DT[1:6, cor(red, green)]
[1] 0.9970501 # should go into row 6, and so on ...
> DT[1:7, cor(red, green)]
[1] 0.9976889
Edit:
I know this can be solved by a loop, but my data table contains about 1 million rows grouped into smaller pieces, so the loop is rather slow, and I thought there might be another possibility.
source
share