I am trying to call a user defined function in order to create a new column that depends on the value of the other columns of my data.table. In simple cases, I do not encounter any error, but when I am either using conditional statement or loops, it looks as if the user defined function receives the entire column as a parameter.
Learning from other cases reported on stack overflow (eg : R data.table user defined function), I understand that this problem can be overcome for if statements using the ifelse function. However, I can't find a solution for the loop statement.
Please, see below the code I want to run that returns the following error message : Error in seq.default(1, a, 1) : 'to' must be of length 1
test <-data.table(a=c(1,2))
f <- function(a) {
out <- 0
for (i in seq(1,a,1)){
out <- out +1
}
return(out)
}
test[,b:=f(a)]
Obviously, f(x)=x but I chose this function for the sake of simplicity. Also, note that replacing seq(1,a,1) by 1:a throws the following warning message : In 1:a : numerical expression has 2 elements: only the first used.
Below is more detailed explanation of the desired behavior.
test <-data.table(a=c(1,2,3),b=c(4,5,6))
f <- function(a,b){
out <-0
for (i in seq(1,a,1)){
out <- out + b^(i)
}
return(out)
}
I would like to have test[,c=f(a,b)] gives :
test
# a b c
# 1 4 4
# 2 5 30 # 5 + 5^2
# 3 6 258 # 6 + 6^2 + 6^3
Is there a way to get the desired behaviour ?
dt<-data.table(a=c(1,2)), calling the functiong<-function(a){return(a)}this waydt[,b:=g(a)]leads to the desire result. The function g only take one parameter (the one of the row), and not the entire row.1,2,3I would like the output column1,2+2^2,3+3^2+3^3calling the functionf <- function(a){out<-0/n for (i in 1:a){out<- out + a^a}/n return(a)}