R - repeat static 1st argument and iterate through 2nd of cor.test in data.table

Task: I have numerical data in a large data.table in R (columns 1:3 are character columns of identifiers); I would like to perform cor.test() where the 1st argument is static (a single column) whilst the 2nd argument iterates through multiple columns.

Info: For the processing to be as time-efficient as possible I would like to perform this in the j parameter of the data.table ( DT[i, j, by] ), unless there are even quicker methods (e.g. would cor.test() within a for loop in the foreach and doparallel libraries be quicker, or could this even be implemented within the data.table itself ?).

However, my attempt returns an error (see below). I can also transpose() the data if required. I appreciate any help offered.


# Install data.table package if not installed and load
if (!require("data.table")) {

# Data (example)
head(DT, n=2)

#>     ID     info1  info2  name1  name2  name3  name4
#>     <char> <char> <char> <dbl>  <dbl>  <dbl>  <dbl>
#> 1:  A100   StuffA StuffB 0.1460 NA     -0.019 0.2102
#> 2:  A101   StuffA StuffC 0.0987 -1.307 -0.174 NA

# Perform correlation test
result <- DT[, cor.test(DT[, "name2"], DT[, 4:ncol(DT)], method = c("pearson"))]


> Error in cor.test.default(DT[, "name2"], DT[, 4:ncol(DT)], method = c("pearson")) : 
>   'x' and 'y' must have the same length

Additional attempts:

# Other attempts to achieve this include:

# 1: lapply() within data.table, returns same error as above
result <- DT[, lapply(.SD, cor.test("name2", 4:ncol(DT)))]

# 2: mapply(), attempt to repeat the single column to get the same dimensions
result <- mapply(function(x, y) cor.test(x, y, method = c("pearson")),
                 x = cbind(rep(DT[, "name2"], times = ncol(DT)-3)),
                 y = DT[, 4:ncol(DT)])