Posts

Showing posts with the label tidyverse

Accumulate values for every possible combination in R

6 Let's say I have data test (dput given) where a list-col say items : test <- structure(list(items = list('a', c('b', 'c'), c('d', 'e'), 'f', c('g', 'h')), ID = c(1,1,1,2,2)), row.names = c(NA, 5L), class = "data.frame") library(tidyverse) test %>% group_by(ID) %>% mutate(dummy = accumulate(items, ~paste(.x, .y))) I am getting an output with list-col like this items ID dummy 1 a 1 a 2 b, c 1 a b, a c 3 d, e 1 a b d, a c e 4 f 2 f 5 g, h 2 f g, f h I would like there to be four items in row3, having each possible combination, i.e. c("a b d", "a b e", "a c d", "a c e")...

Canonical tidyverse method to update some values of a vector from a look-up table

22 3 I frequently need to recode some (not all!) values in a data frame column based off of a look-up table. I'm not satisfied by the ways I know of to solve the problem. I'd like to be able to do it in a clear, stable, and efficient way. Before I write my own function, I'd want to make sure I'm not duplicating something standard that's already out there. ## Toy example data = data.frame( id = 1:7, x = c("A", "A", "B", "C", "D", "AA", ".") ) lookup = data.frame( old = c("A", "D", "."), new = c("a", "d", "!") ) ## desired result # id x # 1 1 a # 2 2 a # 3 3 B # 4 4 C # 5 5 d # 6 6 AA # 7 7 ! I c...