I am trying to create a simple function I can use to change the data type of quite a few columns. For some reason, it says that object 'Dogs' not found when I try to call the function. Any ideas what I'm doing wrong?
The Dogs column is within df_imported, and the code right above works. I'd like to sub in other columns in a function though. Any help would be appreciated.
Your function is using mutate() which uses non-standard evaluation of arguments. For example, in mutate(Dogs = as.integer(Dogs)), Dogs is used as a bare name as if it is a regular object but it is actually a column name in the data frame. mutate() is written to handle this. In your function, you pass in Dogs in the same way but R treats it as an independent object and can't find it.
In the example below, I have written two versions of your function. One uses {{ }} from rlang to handle the non-standard evaluation. The second version accepts "Dogs" with quotes so it is simply a string and is not treated as a object name.
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
library(rlang)
#Using non-standard evaluation
change_type <- function (data_set, columns_to_change)
{
data_set <-
data_set %>%
mutate({{columns_to_change}} := as.integer({{columns_to_change}}))
return(data_set)
}
DF <- data.frame(Dogs=c("1","2","3"))
str(DF)
#> 'data.frame': 3 obs. of 1 variable:
#> $ Dogs: chr "1" "2" "3"
DF <- change_type(data_set = DF,
columns_to_change = Dogs)
str(DF)
#> 'data.frame': 3 obs. of 1 variable:
#> $ Dogs: int 1 2 3
#Not using non-standard evaluation
change_type2 <- function (data_set, columns_to_change) {
data_set[[columns_to_change]] <- as.integer(data_set[[columns_to_change]])
return(data_set)
}
DF <- data.frame(Dogs=c("1","2","3"))
str(DF)
#> 'data.frame': 3 obs. of 1 variable:
#> $ Dogs: chr "1" "2" "3"
DF <- change_type2(data_set = DF,
columns_to_change = "Dogs") #Notice "Dogs" is quoted
str(DF)
#> 'data.frame': 3 obs. of 1 variable:
#> $ Dogs: int 1 2 3