Hi!
How can I refer to a specific variable within a function? The goal is to write a function that will easily allow me to summarize a large dataset by various variables for different analyses.
The problem is figuring out how to translate the variable defined for the function to a reference to an actual dataframe in my environment.
Here is an example of the syntax I've been using
#ex data
place <- c("house", "car", "park", "house", "car")
year <- c("2010", "2010", "2010", "2011", "2011")
var1 <- c(20, 10, 100, 50, 200)
df <- data.frame(place, year, var1)
#what I want to do
function(myvar){
df %>% split(place) %>%
map(mutate, "change"= myvar - lag(myvar)) %>% return()
}
but how do I get myvar to refer to df$var1 as opposed to just "var1"?
Thanks!