Set_names(., paste(., myfuns, sep = "_"))Ĭreated on by the reprex package (v2.0. Here we have to put some effort into creating vectors with the desired names. You can override using the `.groups` argument.Īn alternative way building on the same logic is to use purrr::map2. #> `summarise()` has grouped output by 'cyl'. over2 creates the desired names automatically. The second vector contains the function names, x in sym() whenĪpplying a function to it. Variable names as string, which is why we have to wrap. Loop over two character vectors simultaniously. Summarise(across(all_of(.x), match.fun(.y), With tidyverse, an option is to loop over the column names of interest and the functions in map2 and do a join later library(dplyr) It will have one (or more) rows for each combination of grouping variables if there are no grouping variables, the output will have a single row summarising all observations in the input. Match('disp', names(mtcars)), fsd = match('hp', names(mtcars)))) summarise: Summarise each group to fewer rows Description summarise () creates a new data frame. Or the index can be dynamically generated with match collap(mtcars, ~ cyl + carb, custom = list(fmean = With different functions on different columns, an option is to use collap from collapse library(collapse)Ĭollap(mtcars, ~ cyl + carb, custom = list(fmean = 4, fsd = 5)) #> cyl carb disp_mean disp_sd hp_mean hp_sd Summarise(across(c("disp", "hp"), list(mean = mean, sd = sd))) #> `summarise()` regrouping output by 'cyl' (override with `.groups` argument) Summarise(across(c("disp", "hp"), list(mean = mean, sd = sd))) %>% Finally, I'll be applying this to fairly large dataframes, so I'd rather not calculate the extra columns. I know I can use the names argument to clean those up if I use across, but I'm not confident that across is the correct way. Here's an example of what I want and my best shot so far. Summarize(new_name_2 = function_2(column_2)) Summarize(new_name_1 = function_1(column_1), So, like in a map or apply statement, I want to execute code that ends up looking like: data %>% I'd like to group_by an arbitrary number of variables (thus, across), and then summarize based on arbitrary length (but all the same length) vectors of: I'm trying to do some dplyr programming and having trouble.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |