There is a concept of
- pass by value
- pass by reference
In R objects are immutable so every functions acts like pass by value. You have to assign it to an Object to obtain your answer.
Because data.frame is immutable result of your function have to reassigned to an object to make it work like
iris_dum <- iris
dum_fun <- function(df2){
df2$new <- nrow(df2)
return(df2)
}
iris_dum <- dum_fun(iris_dum) ##
While this is true for a data.frame. There is another object called data.table which is mutable. so you could also write the code like.
iris_dum <- data.table(iris)
dum_fun <- function(df2){
df2[, new := nrow(df2)]
}
dum_fun(iris_dum) ##
iris_dum
and get exactly the same result. Hope I was able to help clarify what is happening.