across

How to combine the across () function with mutate () and case_when () to mutate values in multiple columns according to a condition?

て烟熏妆下的殇ゞ 提交于 2021-02-10 20:51:26
问题 I have demographic data set, which includes the age of people in a household. This is collected via a survey and participants are allowed to refuse providing their age. The result is a data set with one household per row (each with a household ID code), and various household characteristics such as age in the columns. Refused responses as coded as "R", and you could re-create a sample using the code below: df <- list(Household_ID = c("1A", "1B", "1C", "1D", "1E"), AGE1 = c("25", "47", "39",

How to combine the across () function with mutate () and case_when () to mutate values in multiple columns according to a condition?

荒凉一梦 提交于 2021-02-10 20:48:37
问题 I have demographic data set, which includes the age of people in a household. This is collected via a survey and participants are allowed to refuse providing their age. The result is a data set with one household per row (each with a household ID code), and various household characteristics such as age in the columns. Refused responses as coded as "R", and you could re-create a sample using the code below: df <- list(Household_ID = c("1A", "1B", "1C", "1D", "1E"), AGE1 = c("25", "47", "39",

Renaming multiple columns with dplyr rename(across(

你。 提交于 2021-02-04 19:08:53
问题 Hey i'm trying to rename some columsn by adding "Last_" with the new version of dplyr but I keep getting this error Error: `across()` must only be used inside dplyr verbs. this is my code data %>% rename(across(everything(), ~paste0("Last_", .))) dplyr version: v1.0.2 回答1: We can use rename_with instead of rename library(dplyr) library(stringr) data %>% rename_with(~str_c("Last_", .), everything()) Reproducible example data(iris) head(iris) %>% rename_with(~str_c("Last_", .), .cols =

Utilizing functions within across() in dplyr to work with paired-columns

别说谁变了你拦得住时间么 提交于 2021-01-28 12:25:33
问题 set.seed(3) library(dplyr) x <- tibble(Measure = c("Height","Weight","Width","Length"), AD1_1= rpois(4,10), AD1_2= rpois(4,9), AD2_1= rpois(4,10), AD2_2= rpois(4,9), AD3_1= rpois(4,10), AD3_2= rpois(4,9)) Suppose I have data that looks like this. I wish to run a function for each AD, paired with underscored number, i.e., AD1fun, AD2fun,AD3fun. Instead of writing, fun <- function(x,y){x-y} dat %>% mutate(AD1fun = fun(AD1_1,AD1_2), AD2fun = fun(AD2_1,AD2_2), ...) Finding the differences of

Utilizing functions within across() in dplyr to work with paired-columns

不问归期 提交于 2021-01-28 12:17:51
问题 set.seed(3) library(dplyr) x <- tibble(Measure = c("Height","Weight","Width","Length"), AD1_1= rpois(4,10), AD1_2= rpois(4,9), AD2_1= rpois(4,10), AD2_2= rpois(4,9), AD3_1= rpois(4,10), AD3_2= rpois(4,9)) Suppose I have data that looks like this. I wish to run a function for each AD, paired with underscored number, i.e., AD1fun, AD2fun,AD3fun. Instead of writing, fun <- function(x,y){x-y} dat %>% mutate(AD1fun = fun(AD1_1,AD1_2), AD2fun = fun(AD2_1,AD2_2), ...) Finding the differences of

recode using dplyr::mutate across not working in a function

不羁的心 提交于 2021-01-05 07:16:10
问题 I'm trying to use dplyr::mutate(across()) to recode specified columns in a tbl . Using these on their own works fine, but I can't get them to work in a function: library(dplyr) library(tidyr) df1 <- tibble(Q7_1=1:5, Q7_1_TEXT=c("let's","see","grogu","this","week"), Q8_1=6:10, Q8_1_TEXT=rep("grogu",5), Q8_2=11:15, Q8_2_TEXT=c("grogu","is","the","absolute","best")) # this works df2 <- df1 %>% mutate(across(starts_with("Q8") & ends_with("TEXT"), ~recode(., "grogu"="mando"))) # runs without error

Refer to column names dynamically inside mutate statements - dplyr

◇◆丶佛笑我妖孽 提交于 2020-08-22 06:07:11
问题 I stat apologize for the long question, but after quite a while I couldn't figure out a solution myself. I have this toy dataframe set.seed(23) df <- tibble::tibble( id = paste0("00", 1:6), cond = c(1, 1, 2, 2, 3, 3), A_1 = sample(0:9, 6, replace = TRUE), A_2 = sample(0:9, 6, replace = TRUE), A_3 = sample(0:9, 6, replace = TRUE), B_1 = sample(0:9, 6, replace = TRUE), B_2 = sample(0:9, 6, replace = TRUE), B_3 = sample(0:9, 6, replace = TRUE), C_1 = sample(0:9, 6, replace = TRUE), C_2 = sample