Replace the specified column contents in the data frame with the values in the specified vector

Here are the sample code df <- data.frame( A = c(1, 2, 3, 4), B = c(5, 6, NA, 8), C = c(9, NA, -1, 11), D = c(-2, 13, 14, 15), E = c(16, -3, 18, NA) ) replace_vec <- c(100, 200, 300) I want to replace the column 3:5 which the value is… Read More Replace the specified column contents in the data frame with the values in the specified vector

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

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 it… 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?

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 in… 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?

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

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, 410,… 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

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)) list2… 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?

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?