map inside a mutate exploding number of rows in output tibble

Advertisements Say I have data like this: d <- tibble::tribble( ~sit_comfy_sofa_1, ~sit_comfy_sofa_2, ~sit_comfy_sofa_3, ~sit_comfy_sofa_4, ~sit_comfy_couch_1, ~sit_comfy_couch_2, ~sit_comfy_couch_3, ~sit_comfy_couch_4, ~sit_comfy_settee_1, ~sit_comfy_settee_2, ~sit_comfy_settee_3, ~sit_comfy_settee_4, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 0L, 1L,… Read More map inside a mutate exploding number of rows in output tibble

Applying conditional functions on multiple columns of a dataframe, based on values of a variable

Advertisements I have a dataframe with a factor variable identifying my groups (here y), and multiple numerical variables (to simplify, here I only show two x and z): df = tribble( ~x, ~y, ~z, 1, "a", 5, 2, "b", 6, 3, "a", 7, 4, "b", 8, 5, "a", 9, 6, "b", 10 ) I want… Read More Applying conditional functions on multiple columns of a dataframe, based on values of a variable

rename_with doesn't work in purrr::map2 – Error "! the … list contains fewer than 2 elements"

Advertisements Another seemingly easy task where purrr::map2 is giving me a hard time. I have a list of data frames and I have a vector of column names. I now want to rename a certain column in each of the data frames with the respective new column name. So I wanted to simply loop through… Read More rename_with doesn't work in purrr::map2 – Error "! the … list contains fewer than 2 elements"

How to change the class of a column in a list of a list from character to numeric in r?

Advertisements The codes for producing sample dataset and converting from character to numeric is as below: ff = data.frame(a = c(‘1′,’2′,’3’),b = 1:3, c = 5:7) #data.frame is a type of list. fff = list(ff,ff,ff,ff) k = fff %>% map(~map(.x,function(x){x[‘a’] %<>% as.numeric return(x)})) However, the result is something like this…: There are 3 lists appear… Read More How to change the class of a column in a list of a list from character to numeric in r?

Is it possible to interpolate a list of dataframes in r?

Advertisements According to the answer of lhs, https://stackoverflow.com/a/72467827/11124121 #From lhs library(tidyverse) data("population") # create some data to interpolate population_5 <- population %>% filter(year %% 5 == 0) %>% mutate(female_pop = population / 2, male_pop = population / 2) interpolate_func <- function(variable, data) { data %>% group_by(country) %>% # can’t interpolate if only one year filter(n()… Read More Is it possible to interpolate a list of dataframes in r?

Unusual names from across() when using scale() as the .fns

Advertisements I have came across a tricky issue when using the across() and scale() functions together. Here are the sample data: library(tidyverse) roster <- tibble( Student = c("John Davis", "Angela Williams", "Bullwinkle Moose", "David Jones", "Janice Markhammer", "Cheryl Cushing", "Reuven Ytzrhak", "Greg Knox", "Joel England", "Mary Rayburn"), Math = c(502, 600, 412, 358, 495, 512,… Read More Unusual names from across() when using scale() as the .fns