简体   繁体   中英

Add two columns simultaneasouly to a data.table in R

I have this data.table :

DT <- data.table(num=c(1,4,6,7,8,12,13, 15), let=rep(c("A","B"), each=4))

Displayed like this:

   num let
1:   1   A
2:   4   A
3:   6   A
4:   7   A
5:   8   B
6:  12   B
7:  13   B
8:  15   B

And I define this function:

f<-function(x){return(c(x+1,x+2))}

I want to get this:

   let V1 V2
1:   A  2  3
2:   A  5  6
3:   A  7  8
4:   A  8  9
5:   B  9 10
6:   B 13 14
7:   B 14 15
8:   B 16 17

I run this code:

DT[,list(f(num)),by=let]

and

DT[,f(num),by=let]

But I get this:

    let V1
 1:   A  2
 2:   A  5
 3:   A  7
 4:   A  8
 5:   A  3
 6:   A  6
 7:   A  8
 8:   A  9
 9:   B  9
10:   B 13
11:   B 14
12:   B 16
13:   B 10
14:   B 14
15:   B 15
16:   B 17

I would add that I do not want to modify the function f at all.

DT[, as.data.table(matrix(f(num), ncol=2)), by=let]
#   let V1 V2
#1:   A  2  3
#2:   A  5  6
#3:   A  7  8
#4:   A  8  9
#5:   B  9 10
#6:   B 13 14
#7:   B 14 15
#8:   B 16 17

Of course it would be preferable and more efficient to simply change the function, since its design is the problem and not data.table. Your function will always return an atomic vector, regardless of if it is used inside data.table.

f <- function(x) list(x+1, x+2)
DT[, f(num), by=let]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM