Hi everyone,
I have only just started using R and coming from programming in Python, I really like making functions. I want to apply the count method on a dataframe and assign the column name ('col_name') with the argument that is given to the function. My end goal is to loop through the column names (col_names <- colnames(df))) and apply the function to each column individually and to create a new dataframe based on these counts.
count_method = function(col_name = "this_column"){
cur_counts <- df %>% count(col_name)
However, it always gives me an error that the column name is not found in the data frame ( Error: Must group by variables found in .data
. Column col_name
is not found*). I also tried to to apply rlang::sym to the argument and also tried it with the prefix !!. While this option at least didn't give me an error, it didn't select the column and returned a dataframe with the number of rows in the column col_name.
Does anyone know how to help?
I'd also appreciate any suggestions how to achieve my goal in a more efficient way.
Thanks and best,
Eva