简体   繁体   中英

R Data.table assign new column with user defined function

I am concerned by the following example.

library(data.table)

set.seed(1)
table1 <- data.table(a=sample(10,5,TRUE),b=sample(10,5,TRUE))
function1 <- function(a,b){
  a*b+runif(1)
}
table1[,c:=function1(a,b)]
table1[,d:=unlist(mapply(function1,a,b))]
set(table1,NULL,"e",unlist(mapply(function1,table1[,a],table1[,b])))
table1
    a  b         c         d         e
1:  3  9 27.205975 27.176557 27.717619
2:  4 10 40.205975 40.687023 40.991906
3:  6  7 42.205975 42.384104 42.380035
4: 10  7 70.205975 70.769841 70.777445
5:  3  1  3.205975  3.497699  3.934705

I would like to use the syntax I used to create the 'column c' but the number generated by runif(1) is always the same when I use that syntax. I found 2 ways to solve the problem ('column d' and 'column e'), but I clearly prefer the syntax used for 'column c'. Anybody has a solution for me?

Thanks!

In your column c syntax, data.table actually sends two vectors to function1 , but runif(1) is just a number, which R converts to vector of the same value. To avoid this situation pass the length of the vector to runif function (or runif(length(a)) as was suggested)

function1 <- function(a,b, N){
  a*b+runif(N)
}
table1[,c:=function1(a,b, .N)]

Other option would be to evaluate function by row (which I suppose you have in mind)

table1[, id:=.I][, function1(a,b), by = id]

But it's not very efficient

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