davidk
davidk

Reputation: 173

paste() within another function

The first function works and the second one does not, and I am not sure why. I am solely interested in what is happening with the paste() function in this example, as all of the other code works properly. In addition to what is shown below, I have also tried the second function with a comma separator between each value.

Ideally, the list would be as follows, within my function but with the paste() function instead of my listing these values.

X41262.0.0 = i.X41262.0.0, X41262.0.1 = i.X41262.0.1, etc.

fread("ukb33822.csv", select= c("eid", "X2784.0.0", "X2794.0.0", 
                                        "X2804.0.0", "X2814.0.0", "X2834.0.0", 
                                        "X3536.0.0", "X3546.0.0", paste("X41262.0.", 0:65, sep = ""), 
                                        "X3581.0.0"))



biobank[biobank2, on = .(eid), `:=` (X2784.0.0 = i.X2784.0.0, X2794.0.0 = i.X2794.0.0, 
                                 X2804.0.0 = i.X2804.0.0, X2814.0.0 = i.X2814.0.0, 
                                 X2834.0.0 = i.X2834.0.0, X3536.0.0 = i.X3536.0.0, 
                                 X3546.0.0 = i.X3546.0.0, paste("X41262.0.", 0:65, " = ", "i.X41262.0.", 0:65, sep = ""),
                                 X3581.0.0 = i.X3581.0.0)]

Error in 
`[.data.table`(biobank, biobank2, on = .(eid), `:=`(X2784.0.0 = i.X2784.0.0,  : 
  In `:=`(col1=val1, col2=val2, ...) form, all arguments must be named.

Upvotes: 1

Views: 958

Answers (1)

r2evans
r2evans

Reputation: 160447

Not having your data, it's a little contrived, but this might be enough to show you one option:

DT <- data.table(x=1:3)
DT[, c("a", "b", letters[3:5]) := c(list(1, 2), 3:5) ]
DT
#    x a b c d e
# 1: 1 1 2 3 4 5
# 2: 2 1 2 3 4 5
# 3: 3 1 2 3 4 5

In this example:

  • "a" and "b" are your already-known names, e.g., "X2784.0.0", "X2794.0.0", etc
  • letters[3:5] are names you need to create programmatically, e.g., paste0("X41262.0.", 0:65)
  • 1 and 2 are your already-known values, e.g., i.X2784.0.0, i.X2794.0.0, etc
  • 3:5 are values you determine programmatically

It is not clear to me where your other values are found ...

If they are in the enclosing environment (and not within the actual table), then perhaps:

x1 <- 3:5
x2 <- 13:15
x3 <- 33:35
e <- environment()
DT[, c("a", "b", paste0("x", 1:3)) := c(list(1, 2), mget(paste0("x", 1:3), envir=e))]
#    x a b x1 x2 x3
# 1: 1 1 2  3 13 33
# 2: 2 1 2  4 14 34
# 3: 3 1 2  5 15 35

where paste0("x", 1:3) is forming the variable names, and mget(...) actually retrieves them. You might need to define e as I have here, if they are not visible from data.table's search path.

If they are already in the data.table, then you might be able to do something with this:

DT <- data.table(x1=1:3, x2=11:13, x3=21:23)
DT[, c("a", "b", paste0("y", 1:3)) := c(list(1, 2), DT[, paste0("x", 1:3), with=FALSE]) ]
#    x1 x2 x3 a b y1 y2 y3
# 1:  1 11 21 1 2  1 11 21
# 2:  2 12 22 1 2  2 12 22
# 3:  3 13 23 1 2  3 13 23

where paste0("y", 1:3) forms the names you want them to be, and paste0("x", 1:3) forms the other columns' names as they exist before this call.

Upvotes: 2

Related Questions