mutate

R dplyr: rowwise + mutate (+glue) - how to get/refer row content?

别等时光非礼了梦想. 提交于 2020-01-25 00:38:08
问题 The simple example of input data: dataset <- data.frame("part1" = c("a", "b", "c"), "part2" = c("x", "y", "z"), "caption" = c("{part1} {part2}", "{part2} {part1}", "{part2} {part1} {part2}"), stringsAsFactors = F) Expected results: # A tibble: 3 x 3 part1 part2 caption <chr> <chr> <chr> 1 a x a x 2 b y y b 3 c z z c z The below code doesn't work, because . refers to the whole dataset , instead of data of the whole row content: dataset %>% rowwise() %>% mutate("caption" = glue::glue_data(.,

How to apply a “complicated” user defined function on each element of a tibble

自古美人都是妖i 提交于 2020-01-17 06:13:08
问题 I have searched high and low for the answer to this (seemingly simple) problem, but came up empty so I hope someone can help me or point me in the right direction. I have a fairly complicated submodel that I want to apply to a dataset, but if I just use use mutate I get an error Variables must be length 1 or 21. adding rowwise() doesnt seem to impact it. Let me use the following silly illustration of the problem: myData <- tibble(x=10:20, y=c("a", "b","a", "b","a", "b","a", "b","a", "b","a"))

Vuejs Mutating Object passed as a prop

久未见 提交于 2020-01-16 08:55:10
问题 If I'm passing (a reference to) an Object as a prop is it OK to mutate values in the prop? I'm developing a web app which will require a lot of values to be passed to a component, and I'm trying to find the best way of passing the values to the component and back to the parent. From everything I've read mutating a prop is the wrong way to do things, because next time the component is updated the values are passed back to the child component overwriting the mutations. But only the reference to

R - dplyr - mutate_if multiple conditions

余生颓废 提交于 2020-01-15 08:55:06
问题 I want to mutate a column based on multiple conditions. For example, for each column where the max is 5 and the column name contains "xy", apply a function. df <- data.frame( xx1 = c(0, 1, 2), xy1 = c(0, 5, 10), xx2 = c(0, 1, 2), xy2 = c(0, 5, 10) ) > df xx1 xy1 xx2 xy2 1 0 0 0 0 2 1 5 1 5 3 2 10 2 10 df2 <- df %>% mutate_if(~max(.)==10, as.character) > str(df2) 'data.frame': 3 obs. of 4 variables: $ xx1: num 0 1 2 $ xy1: chr "0" "5" "10" $ xx2: num 0 1 2 $ xy2: chr "0" "5" "10" #function

loop to multiply across columns

梦想的初衷 提交于 2020-01-15 03:12:34
问题 I have a data frame with columns labeled sales1 , sales2 , price1 , price2 and I want to calculate revenues by multiplying sales1 * price1 and so-on across each number in an iterative fashion. data <- data_frame( "sales1" = c(1, 2, 3), "sales2" = c(2, 3, 4), "price1" = c(3, 2, 2), "price2" = c(3, 3, 5)) data # A tibble: 3 x 4 # sales1 sales2 price1 price2 # <dbl> <dbl> <dbl> <dbl> #1 1 2 3 3 #2 2 3 2 3 #3 3 4 2 5 Why doesn't the following code work? data %>% mutate ( for (i in seq_along(1:2))

r mutate_each function is deprecated

送分小仙女□ 提交于 2020-01-04 02:03:18
问题 I use the function mutate_each from the tidyverse package and I get a message this function is deprecated. I would like to use the other functions that are not deprecated to change field types. Below is a reproducible example of how I currently employ mutate_each. library(tidyverse) set.seed(123) df <- data.frame(FirstName = sample(LETTERS[1:2],size=3, replace=TRUE), LastName = sample(LETTERS[3:6],size=3, replace=TRUE), StartDate = c("1/31/2000","2/1/2000","3/1/2000"), EndDate = c("1/31/2010"

r mutate_each function is deprecated

点点圈 提交于 2020-01-04 02:03:12
问题 I use the function mutate_each from the tidyverse package and I get a message this function is deprecated. I would like to use the other functions that are not deprecated to change field types. Below is a reproducible example of how I currently employ mutate_each. library(tidyverse) set.seed(123) df <- data.frame(FirstName = sample(LETTERS[1:2],size=3, replace=TRUE), LastName = sample(LETTERS[3:6],size=3, replace=TRUE), StartDate = c("1/31/2000","2/1/2000","3/1/2000"), EndDate = c("1/31/2010"

Sorting the values of column in ascending order in R

谁说胖子不能爱 提交于 2019-12-31 05:33:07
问题 The script below is a data frame of four columns. My need is that I want to take a pair of values(a1,a2) at a time. The column "a3" is such that if you check a pair say (a1,a2), as you span the data, the pair's value is arranged in ascending order. If there is a duplicate of the pair present in the table, I want the "a4" column values to be arranged just like the corresponding "a3" column in ascending order for the particular (a1,a2) value. Say the first (a1,a2) pair ("A","D"), the pair

How to use custom functions in mutate (dplyr)?

北城余情 提交于 2019-12-29 08:38:22
问题 I'm rewriting all my code using dplyr, and need help with mutate / mutate_at function. All I need is to apply custom function to two columns in my table. Ideally, I would reference these columns by their indices, but now I can't make it work even referencing by names. The function is: binom.test.p <- function(x) { if (is.na(x[1])|is.na(x[2])|(x[1]+x[2])<10) { return(NA) } else { return(binom.test(x, alternative="two.sided")$p.value) } } My data: table <- data.frame(geneId=c("a", "b", "c", "d"

tidyr; %>% group_by() mutate(foo = fill() )

冷暖自知 提交于 2019-12-25 10:16:35
问题 I'm struggling to create a new variable to indicate what letter, LET , some groups, grp , within id, id , begin with. In the following I'll illustrate my question. I have data like this, library(dplyr); library(tidyr) df <- tibble(id = rep(0:1, c(7, 10)), grp = rep(c(0,1,0,1,2), c(3,4,2,5,3)), LET = rep(c('A', 'B', 'A', 'B', 'A', 'B'), c(1,4, 3, 3, 4, 2))) #> # A tibble: 17 x 3 #> id grp LET #> <int> <dbl> <chr> #> 1 0 0 A #> 2 0 0 B #> 3 0 0 B #> 4 0 1 B #> 5 0 1 B #> 6 0 1 A #> 7 0 1 A #> 8