I'm trying to create a function that will take 2 variables from a dataset, and map their distinct values side by side, after which it will write the out to a csv file. I'll be using dplyr's distinct function for getting the unique values.
map_table <- function(df, var1, var2){
df_distinct <- df %>% distinct(var1, var2)
write.csv(df_distinct, 'var1.csv')
}
map_table(iris, Species, Petal.Width)
1) map_table(iris, Species, Petal.Width) doesn't produce what I want. It should produce 27 rows of data, instead I'm getting 150 rows of data.
2) How can I name the csv file after the input of var1? So if var1 = 'Sepal.Length', the name of the file should be 'Sepal.Length.csv'