i have a question for another problem, but i tried breaking it down to a simpler code, so that my initial problem is visible. I tried making a dataframe and a function computing the sum as follows:
df <- data.frame(x=c(1,2,3),y=c(1,2,3))
fun <- function(data,x,y){
z <- sum(data$x) + sum(data$y)
return(z)
}
fun(data= df,x = df$x,y = df$y)
[1] 12
The code gives me the expected sum 12. Changing the colnames of the df dataframe to e.g "r" and "t" returns 0, even if i specify the arguments in the function. What is wrong?
df <- data.frame(r=c(1,2,3),t=c(1,2,3))
fun <- function(data,x,y){
z <- sum(data$x) + sum(data$y)
return(z)
}
fun(data= df,x = df$r,y = df$t)
[1] 0
Thanks in advance.