How to create multiple columns at once using same function with different argument values in R?

Advertisements I have the following data: library(tidyverse) library(slider) data <- tibble::tribble( ~group, ~value, "0001", 2L, "0001", -2L, "0001", 7L, "0001", -9L, "0001", 2L, "0001", -4L, "0001", 6L, "0001", -10L, "0001", 4L, "0001", -3L, "0002", -2L, "0002", 5L, "0002", -2L, "0002", 4L, "0002", -2L, "0002", 2L, "0002", -3L, "0002", 3L, "0002", -1L, "0002", 5L, "0003", -2L,… Read More How to create multiple columns at once using same function with different argument values in R?

1:nrow(.) can't refer to former defined object with pipe %>%

Advertisements For some reason I wanted to run 1:nrow(.), where . refers to the dataframe defined before. It is something like this: library(tidyverse) data.frame(1:10) %>% 1:nrow(.) I get the error Error in :(., 1, nrow(.)) : 3 arguments passed to ‘:’ which requires 2. But data.frame(1:10) %>% nrow(.) works as expected. I tried to change… Read More 1:nrow(.) can't refer to former defined object with pipe %>%

How to merge 2 dataframes and fill blank spaces with previous dataframe values in R

Advertisements I have two datasets like the following: df1: gene_id pvalue ENSG00000000003 0.04 ENSG00000000419 0.004 ENSG00000111780 0.0004 ENSG00000093100 0.00004 df2: gene_id gene_name ENSG00000000003 TSPAN6 ENSG00000000419 DPM1 ENSG00000111780 ENSG00000093100 I want to join the two dataframes by the gene_id column and fill the gene_name blanks with the corresponding gene_id. The result I would like to obtain… Read More How to merge 2 dataframes and fill blank spaces with previous dataframe values in R

Extract leading numbers from string, but length varies R

Advertisements I have a column which contains a character string containing letters and numbers. The string always starts with one or two numbers, followed by multiple characters. I am trying to separate the string based on where that first character is. have <- tribble( ~string, ’12main’, ‘6six’, ’42go’, ‘5to9’ ) want <- tribble( ~prefix, ~rest,… Read More Extract leading numbers from string, but length varies R

Calculate the lagged differences between consecutive dates in vectors

Advertisements The sample dataset is given as below: v = data.frame(group = c(1,1,2,3,3),date = as.Date(c(’01-01-2000′,’01-01-2001′,’01-05-2000′,’02-07-2000′,’01-01-2008′), "%d-%m-%Y")) v%>% group_by(group ) %>% mutate(difference_day = ifelse(n() == 2, c(0,diff(date )), difftime(date ,as.Date(’31-12-2021′, "%d-%m-%Y"),units=’days’))) My desirable result is : group difference_day 1 0 1 365 2 7915 3 0 3 2740 In the above code, if the length of… Read More Calculate the lagged differences between consecutive dates in vectors

How to inplace the result to the original dataframe in r tidyverse?

Advertisements I would like to calculate the day difference by the number of row of groups: v = data.frame(group = c(‘a’,’a’,’b’,’c’,’c’),days_difference = c(0,56,0,0,23)) v %>% group_by(group) %>% filter(n() == 1) %>% mutate(days_difference = (difftime(as.Date(’01-01-2020′, "%d-%m-%Y"),as.Date(’04-06-2018′,"%d-%m-%Y")))) In the above code, if the length of groups is equal to one, then the days_difference will change from 0… Read More How to inplace the result to the original dataframe in r tidyverse?

tidyverse- Is pivot_wider() only way to summarize selecting specific row values?

Advertisements I need to summarize an index of testing results from tidy data. For each group, I need to do a weighted sum of specific values to return a index value. I’m used to using group_by() and summarise() and to subset with the format df$value[var==’A’], but I can’t get that way to work. I can… Read More tidyverse- Is pivot_wider() only way to summarize selecting specific row values?