tidyeval

Tidy Evaluation not working with mutate and stringr

守給你的承諾、 提交于 2021-02-11 13:43:16
问题 I've trying to use Tidy Eval and Stringr togheter inside a mutate pipe, but every time I run it it gives me an undesirable result. Instead of changing the letter 'a' for the letter 'X', it overwrite the entire vector with the column name, as you can see in the example below, that uses the IRIS dataset. text_col="Species" iris %>% mutate({{text_col}} := str_replace_all({{text_col}}, pattern = "a", replacement = "X")) result: structure(list(Sepal.Length = c(5.1, 4.9, 4.7, 4.6, 5, 5.4, 4.6, 5, 4

Writing a custom case_when function to use in dplyr mutate using tidyeval

萝らか妹 提交于 2021-02-11 13:32:12
问题 I'm trying to write a custom case_when function to use inside dplyr. I've been reading through the tidyeval examples posted in other questions, but still can't figure out how to make it work. Here's a reprex: df1 <- data.frame(animal_1 = c("Horse", "Pig", "Chicken", "Cow", "Sheep"), animal_2 = c(NA, NA, "Horse", "Sheep", "Chicken")) translate_title <- function(data, input_col, output_col) { mutate(data, !!output_col := case_when( input_col == "Horse" ~ "Cheval", input_col == "Pig" ~ "Рorc",

Access the column names in the `mutate_at` to use it for subseting a list

非 Y 不嫁゛ 提交于 2021-02-09 13:58:33
问题 I am trying to recode several variables but with different recode schemes. The recoding scheme is saved in a list where each element is a named vector of the form old = new . Each element is the recoding scheme for each variable in the data frame I am using the mutate_at function and the recode . I think that the problem is that I cannot extract the variable name to use it to get the correct recoding scheme from the list I tried deparse(substitute(.)) as in here and also this didn;t help Also

Access the column names in the `mutate_at` to use it for subseting a list

拟墨画扇 提交于 2021-02-09 13:57:29
问题 I am trying to recode several variables but with different recode schemes. The recoding scheme is saved in a list where each element is a named vector of the form old = new . Each element is the recoding scheme for each variable in the data frame I am using the mutate_at function and the recode . I think that the problem is that I cannot extract the variable name to use it to get the correct recoding scheme from the list I tried deparse(substitute(.)) as in here and also this didn;t help Also

Programming with tidyeval: The mutate function after tidyr::unite(col = !!col)

倾然丶 夕夏残阳落幕 提交于 2021-02-08 04:54:20
问题 So I want to make a function with unite() from tidyr, but it does not seem to work.. library(dplyr, warn.conflicts = FALSE) library(tidyr, warn.conflicts = FALSE) library(stringr, warn.conflicts = FALSE) mtcars %>% as_tibble() %>% select(mpg , cyl) %>% mutate_all(as.character) %>% unite(col = hello, sep = "/") %>% mutate(hello = str_replace(hello, "/", "")) #> # A tibble: 32 x 1 #> hello #> <chr> #> 1 216 #> 2 216 #> 3 22.84 #> 4 21.46 #> 5 18.78 #> 6 18.16 #> 7 14.38 #> 8 24.44 #> 9 22.84 #>

Programming with tidyeval: The mutate function after tidyr::unite(col = !!col)

白昼怎懂夜的黑 提交于 2021-02-08 04:54:09
问题 So I want to make a function with unite() from tidyr, but it does not seem to work.. library(dplyr, warn.conflicts = FALSE) library(tidyr, warn.conflicts = FALSE) library(stringr, warn.conflicts = FALSE) mtcars %>% as_tibble() %>% select(mpg , cyl) %>% mutate_all(as.character) %>% unite(col = hello, sep = "/") %>% mutate(hello = str_replace(hello, "/", "")) #> # A tibble: 32 x 1 #> hello #> <chr> #> 1 216 #> 2 216 #> 3 22.84 #> 4 21.46 #> 5 18.78 #> 6 18.16 #> 7 14.38 #> 8 24.44 #> 9 22.84 #>

Programming with tidyeval: The mutate function after tidyr::unite(col = !!col)

拈花ヽ惹草 提交于 2021-02-08 04:54:06
问题 So I want to make a function with unite() from tidyr, but it does not seem to work.. library(dplyr, warn.conflicts = FALSE) library(tidyr, warn.conflicts = FALSE) library(stringr, warn.conflicts = FALSE) mtcars %>% as_tibble() %>% select(mpg , cyl) %>% mutate_all(as.character) %>% unite(col = hello, sep = "/") %>% mutate(hello = str_replace(hello, "/", "")) #> # A tibble: 32 x 1 #> hello #> <chr> #> 1 216 #> 2 216 #> 3 22.84 #> 4 21.46 #> 5 18.78 #> 6 18.16 #> 7 14.38 #> 8 24.44 #> 9 22.84 #>

What is the difference between . and .data?

て烟熏妆下的殇ゞ 提交于 2021-02-06 11:11:29
问题 I'm trying to develop a deeper understanding of using the dot (".") with dplyr and using the .data pronoun with dplyr . The code I was writing that motivated this post, looked something like this: cat_table <- tibble( variable = vector("character"), category = vector("numeric"), n = vector("numeric") ) for(i in c("cyl", "vs", "am")) { cat_stats <- mtcars %>% count(.data[[i]]) %>% mutate(variable = names(.)[1]) %>% rename(category = 1) cat_table <- bind_rows(cat_table, cat_stats) } # A tibble:

What is the difference between . and .data?

馋奶兔 提交于 2021-02-06 11:08:35
问题 I'm trying to develop a deeper understanding of using the dot (".") with dplyr and using the .data pronoun with dplyr . The code I was writing that motivated this post, looked something like this: cat_table <- tibble( variable = vector("character"), category = vector("numeric"), n = vector("numeric") ) for(i in c("cyl", "vs", "am")) { cat_stats <- mtcars %>% count(.data[[i]]) %>% mutate(variable = names(.)[1]) %>% rename(category = 1) cat_table <- bind_rows(cat_table, cat_stats) } # A tibble:

What is the difference between . and .data?

有些话、适合烂在心里 提交于 2021-02-06 11:07:24
问题 I'm trying to develop a deeper understanding of using the dot (".") with dplyr and using the .data pronoun with dplyr . The code I was writing that motivated this post, looked something like this: cat_table <- tibble( variable = vector("character"), category = vector("numeric"), n = vector("numeric") ) for(i in c("cyl", "vs", "am")) { cat_stats <- mtcars %>% count(.data[[i]]) %>% mutate(variable = names(.)[1]) %>% rename(category = 1) cat_table <- bind_rows(cat_table, cat_stats) } # A tibble: