Hi all,
I have wriiten a small function to create a new column to a dataframe. But I am not able to execute it. Can anyone help me?
iris_dum <- iris
dum_fun <- function(df2){
df2$new <- nrow(df2)
}
dum_fun(iris_dum) ## After executing this, a new column "new" should be created to iris$dum
Hello,
Try the below. It should work. It just needs the return
as it needs to know what to return.
iris_dum <- iris
dum_fun <- function(x){
x$new <- nrow(x)
return(x)
}
df_new <- dum_fun(iris_dum) ## After executing this, a new column "new" should be created to iris$dum
df_new
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species new
#> 1 5.1 3.5 1.4 0.2 setosa 150
#> 2 4.9 3.0 1.4 0.2 setosa 150
#> 3 4.7 3.2 1.3 0.2 setosa 150
#> 4 4.6 3.1 1.5 0.2 setosa 150
#> 5 5.0 3.6 1.4 0.2 setosa 150
#> 6 5.4 3.9 1.7 0.4 setosa 150
#> 7 4.6 3.4 1.4 0.3 setosa 150
#> 8 5.0 3.4 1.5 0.2 setosa 150
#> 9 4.4 2.9 1.4 0.2 setosa 150
#> 10 4.9 3.1 1.5 0.1 setosa 150
#> 11 5.4 3.7 1.5 0.2 setosa 150
#> 12 4.8 3.4 1.6 0.2 setosa 150
#> 13 4.8 3.0 1.4 0.1 setosa 150
#> 14 4.3 3.0 1.1 0.1 setosa 150
#> 15 5.8 4.0 1.2 0.2 setosa 150
#> 16 5.7 4.4 1.5 0.4 setosa 150
#> 17 5.4 3.9 1.3 0.4 setosa 150
#> 18 5.1 3.5 1.4 0.3 setosa 150
#> 19 5.7 3.8 1.7 0.3 setosa 150
#> 20 5.1 3.8 1.5 0.3 setosa 150
#> 21 5.4 3.4 1.7 0.2 setosa 150
#> 22 5.1 3.7 1.5 0.4 setosa 150
#> 23 4.6 3.6 1.0 0.2 setosa 150
#> 24 5.1 3.3 1.7 0.5 setosa 150
#> 25 4.8 3.4 1.9 0.2 setosa 150
#> 26 5.0 3.0 1.6 0.2 setosa 150
#> 27 5.0 3.4 1.6 0.4 setosa 150
#> 28 5.2 3.5 1.5 0.2 setosa 150
#> 29 5.2 3.4 1.4 0.2 setosa 150
#> 30 4.7 3.2 1.6 0.2 setosa 150
#> 31 4.8 3.1 1.6 0.2 setosa 150
#> 32 5.4 3.4 1.5 0.4 setosa 150
#> 33 5.2 4.1 1.5 0.1 setosa 150
#> 34 5.5 4.2 1.4 0.2 setosa 150
#> 35 4.9 3.1 1.5 0.2 setosa 150
#> 36 5.0 3.2 1.2 0.2 setosa 150
#> 37 5.5 3.5 1.3 0.2 setosa 150
#> 38 4.9 3.6 1.4 0.1 setosa 150
#> 39 4.4 3.0 1.3 0.2 setosa 150
#> 40 5.1 3.4 1.5 0.2 setosa 150
#> 41 5.0 3.5 1.3 0.3 setosa 150
#> 42 4.5 2.3 1.3 0.3 setosa 150
#> 43 4.4 3.2 1.3 0.2 setosa 150
#> 44 5.0 3.5 1.6 0.6 setosa 150
#> 45 5.1 3.8 1.9 0.4 setosa 150
#> 46 4.8 3.0 1.4 0.3 setosa 150
#> 47 5.1 3.8 1.6 0.2 setosa 150
#> 48 4.6 3.2 1.4 0.2 setosa 150
#> 49 5.3 3.7 1.5 0.2 setosa 150
#> 50 5.0 3.3 1.4 0.2 setosa 150
#> 51 7.0 3.2 4.7 1.4 versicolor 150
#> 52 6.4 3.2 4.5 1.5 versicolor 150
#> 53 6.9 3.1 4.9 1.5 versicolor 150
#> 54 5.5 2.3 4.0 1.3 versicolor 150
#> 55 6.5 2.8 4.6 1.5 versicolor 150
#> 56 5.7 2.8 4.5 1.3 versicolor 150
#> 57 6.3 3.3 4.7 1.6 versicolor 150
#> 58 4.9 2.4 3.3 1.0 versicolor 150
#> 59 6.6 2.9 4.6 1.3 versicolor 150
#> 60 5.2 2.7 3.9 1.4 versicolor 150
#> 61 5.0 2.0 3.5 1.0 versicolor 150
#> 62 5.9 3.0 4.2 1.5 versicolor 150
#> 63 6.0 2.2 4.0 1.0 versicolor 150
#> 64 6.1 2.9 4.7 1.4 versicolor 150
#> 65 5.6 2.9 3.6 1.3 versicolor 150
#> 66 6.7 3.1 4.4 1.4 versicolor 150
#> 67 5.6 3.0 4.5 1.5 versicolor 150
#> 68 5.8 2.7 4.1 1.0 versicolor 150
#> 69 6.2 2.2 4.5 1.5 versicolor 150
#> 70 5.6 2.5 3.9 1.1 versicolor 150
#> 71 5.9 3.2 4.8 1.8 versicolor 150
#> 72 6.1 2.8 4.0 1.3 versicolor 150
#> 73 6.3 2.5 4.9 1.5 versicolor 150
#> 74 6.1 2.8 4.7 1.2 versicolor 150
#> 75 6.4 2.9 4.3 1.3 versicolor 150
#> 76 6.6 3.0 4.4 1.4 versicolor 150
#> 77 6.8 2.8 4.8 1.4 versicolor 150
#> 78 6.7 3.0 5.0 1.7 versicolor 150
#> 79 6.0 2.9 4.5 1.5 versicolor 150
#> 80 5.7 2.6 3.5 1.0 versicolor 150
#> 81 5.5 2.4 3.8 1.1 versicolor 150
#> 82 5.5 2.4 3.7 1.0 versicolor 150
#> 83 5.8 2.7 3.9 1.2 versicolor 150
#> 84 6.0 2.7 5.1 1.6 versicolor 150
#> 85 5.4 3.0 4.5 1.5 versicolor 150
#> 86 6.0 3.4 4.5 1.6 versicolor 150
#> 87 6.7 3.1 4.7 1.5 versicolor 150
#> 88 6.3 2.3 4.4 1.3 versicolor 150
#> 89 5.6 3.0 4.1 1.3 versicolor 150
#> 90 5.5 2.5 4.0 1.3 versicolor 150
#> 91 5.5 2.6 4.4 1.2 versicolor 150
#> 92 6.1 3.0 4.6 1.4 versicolor 150
#> 93 5.8 2.6 4.0 1.2 versicolor 150
#> 94 5.0 2.3 3.3 1.0 versicolor 150
#> 95 5.6 2.7 4.2 1.3 versicolor 150
#> 96 5.7 3.0 4.2 1.2 versicolor 150
#> 97 5.7 2.9 4.2 1.3 versicolor 150
#> 98 6.2 2.9 4.3 1.3 versicolor 150
#> 99 5.1 2.5 3.0 1.1 versicolor 150
#> 100 5.7 2.8 4.1 1.3 versicolor 150
#> 101 6.3 3.3 6.0 2.5 virginica 150
#> 102 5.8 2.7 5.1 1.9 virginica 150
#> 103 7.1 3.0 5.9 2.1 virginica 150
#> 104 6.3 2.9 5.6 1.8 virginica 150
#> 105 6.5 3.0 5.8 2.2 virginica 150
#> 106 7.6 3.0 6.6 2.1 virginica 150
#> 107 4.9 2.5 4.5 1.7 virginica 150
#> 108 7.3 2.9 6.3 1.8 virginica 150
#> 109 6.7 2.5 5.8 1.8 virginica 150
#> 110 7.2 3.6 6.1 2.5 virginica 150
#> 111 6.5 3.2 5.1 2.0 virginica 150
#> 112 6.4 2.7 5.3 1.9 virginica 150
#> 113 6.8 3.0 5.5 2.1 virginica 150
#> 114 5.7 2.5 5.0 2.0 virginica 150
#> 115 5.8 2.8 5.1 2.4 virginica 150
#> 116 6.4 3.2 5.3 2.3 virginica 150
#> 117 6.5 3.0 5.5 1.8 virginica 150
#> 118 7.7 3.8 6.7 2.2 virginica 150
#> 119 7.7 2.6 6.9 2.3 virginica 150
#> 120 6.0 2.2 5.0 1.5 virginica 150
#> 121 6.9 3.2 5.7 2.3 virginica 150
#> 122 5.6 2.8 4.9 2.0 virginica 150
#> 123 7.7 2.8 6.7 2.0 virginica 150
#> 124 6.3 2.7 4.9 1.8 virginica 150
#> 125 6.7 3.3 5.7 2.1 virginica 150
#> 126 7.2 3.2 6.0 1.8 virginica 150
#> 127 6.2 2.8 4.8 1.8 virginica 150
#> 128 6.1 3.0 4.9 1.8 virginica 150
#> 129 6.4 2.8 5.6 2.1 virginica 150
#> 130 7.2 3.0 5.8 1.6 virginica 150
#> 131 7.4 2.8 6.1 1.9 virginica 150
#> 132 7.9 3.8 6.4 2.0 virginica 150
#> 133 6.4 2.8 5.6 2.2 virginica 150
#> 134 6.3 2.8 5.1 1.5 virginica 150
#> 135 6.1 2.6 5.6 1.4 virginica 150
#> 136 7.7 3.0 6.1 2.3 virginica 150
#> 137 6.3 3.4 5.6 2.4 virginica 150
#> 138 6.4 3.1 5.5 1.8 virginica 150
#> 139 6.0 3.0 4.8 1.8 virginica 150
#> 140 6.9 3.1 5.4 2.1 virginica 150
#> 141 6.7 3.1 5.6 2.4 virginica 150
#> 142 6.9 3.1 5.1 2.3 virginica 150
#> 143 5.8 2.7 5.1 1.9 virginica 150
#> 144 6.8 3.2 5.9 2.3 virginica 150
#> 145 6.7 3.3 5.7 2.5 virginica 150
#> 146 6.7 3.0 5.2 2.3 virginica 150
#> 147 6.3 2.5 5.0 1.9 virginica 150
#> 148 6.5 3.0 5.2 2.0 virginica 150
#> 149 6.2 3.4 5.4 2.3 virginica 150
#> 150 5.9 3.0 5.1 1.8 virginica 150
Created on 2020-12-15 by the reprex package (v0.3.0)
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.
system
Closed
January 5, 2021, 2:15pm
4
This topic was automatically closed 21 days after the last reply. New replies are no longer allowed. If you have a query related to it or one of the replies, start a new topic and refer back with a link.