Find the last element by group in a data.table without max()

Advertisements I’m trying to find the last element in a data.table by group in a time efficient way. I have a solution that works: library(data.table) Data <- data.table(id = c(rep("a", 2), rep("b",3)), time = c(1:2, 1:3)) Data[, lastobs := max(time), by = id] Data <- Data[time == lastobs] Data[, lastobs := NULL] but the max()… Read More Find the last element by group in a data.table without max()

Split string and find unique characters for each row using data.table

Advertisements Suppose I have a data table: dt <- data.table(x = c("a,b,b,c,NA","b,b,NA,c","a,b,c,c,c,d")) dt x 1: a,b,b,c,NA 2: b,b,NA,c 3: a,b,c,c,c,d Now for each row I would like to split the x column, extract all unique characters and paste them together so I get: x 1: a,b,c,NA 2: b,NA,c 3: a,b,c,d I have tried this so… Read More Split string and find unique characters for each row using data.table

Recovering lm() results from a data.table groupby models

Advertisements I need to analyse the output of a series of lm() output from a data.table group_by regressions: library(data.table) x <- c(1:5,66:70,101:110) y <- 31:50 g <- c( 1,1,1,1,1,1,1,1,1,1 ,2,2,2,2,2,2,2,2,2,2) dt <- data.table(x,y,g) mod <- dt[ , .(model = .(lm(x~y, .SD))) , by = g] mod has the correct models for each group g. Now… Read More Recovering lm() results from a data.table groupby models

Combine multiple rows based on ID column

Advertisements I have the following df: df<- structure(list(X18.digit.contact.id = c("0034y00002kIZ3rAAG", "0034y00002kIZ3rAAG", "0034y00002kIZ3rAAG", "0034y00002PpX11AAF", "0034y00002PpX11AAF", "0034y00002PpX11AAF", "0034y00002jHjYKAA0", "0034y00002jHjYKAA0", "0034y00002jHjYKAA0"), `Fitness Goal` = c(2L, NA, NA, -1L, NA, NA, NA, 1L, NA), `Nutrition/Hydration Goal` = c(NA, NA, NA, NA, 0L, NA, 2L, NA, NA), `Lifestyle Goal` = c(NA, NA, 2L, NA, NA, 0L, NA, NA, 1L)), class… Read More Combine multiple rows based on ID column

concatenate unique strings of variable in data.table by repeated values of another variable in data.table

Advertisements I have a data.table that has one column, we’ll call it country with repeated values, and another column (survey) that has unique strings for each observation. I want to create a new variable that has the pasted value of all the survey strings for an individual country. I’ve done it with an index and… Read More concatenate unique strings of variable in data.table by repeated values of another variable in data.table

Split values of a data.table column and select the lowest number or NA if all are "NA"

Advertisements I have the following data table in R: library(data.table) dt <- data.table(x = c(NA, NA, NA, NA, NA, NA, NA, "1,2", "NA,3", "NA,NA,NA", "NA,NA,NA,NA", "NA,NA", "NA,4,NA,1", "NA,NA,NA", "NA,NA")) x 1: <NA> 2: <NA> 3: <NA> 4: <NA> 5: <NA> 6: <NA> 7: <NA> 8: 1,2 9: NA,3 10: NA,NA,NA 11: NA,NA,NA,NA 12: NA,NA 13:… Read More Split values of a data.table column and select the lowest number or NA if all are "NA"