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

Add a column from one dataframe in a list to another dataframe in another list with map2

Advertisements I have two lists with different dataframes (original data has 70 dataframes for each list, totalising 2 million rows). df1 <- data.frame(colA = LETTERS[1:5], colB = seq(1,5)) df2 <- data.frame(colA = LETTERS[6:10], colB = seq(6,10)) list1 <- list(df1,df2) df3 <- data.frame(colA = LETTERS[1:5], colB = seq(11,15)) df4 <- data.frame(colA = LETTERS[6:10], colB = seq(16,20))… Read More Add a column from one dataframe in a list to another dataframe in another list with map2

How do I make this R code to format integers with leading zero more succinct?

Advertisements I have created a function called interval which takes two numbers as input between 1 and 12 and if the number is less than 10, it appends a 0 to the front. e.g. 4 becomes 04, but 11 stays 11. interval <- function(month_start = 1, month_end = 12){ month_range <- as.character(c(month_start:month_end)) month_range_char <- month_range… Read More How do I make this R code to format integers with leading zero more succinct?

Using a dataframe to input values to a function and aggregating the outputs

Advertisements I’m very new to R and completely lost. I’ve have a function that will aggregate my data, but I can’t work out how to add the results to some new dataframe and how to loop an input to get the results? My input looks like this input = structure(list(V1 = c("Team_2022", "Team_2022", "Team_2022"), V2… Read More Using a dataframe to input values to a function and aggregating the outputs

Using purrr:map2 to perform regression where the predictor and the criterion are stored in different objects

Advertisements for the purposes of this question, let’s create the following setup: mtcars %>% group_split(carb) %>% map(select, mpg) -> criterion mtcars %>% group_split(carb) %>% map(select, qsec) -> predictor This code will create two lists of length 6. What I want to do is to perform 6 linear regressions within each of these 6 groups. I… Read More Using purrr:map2 to perform regression where the predictor and the criterion are stored in different objects