Is `<-` also for data.frames using call by reference sometimes?

I have a simple problem that I wanted to solve using data.table. I was surprised about the following behaviour, as I thought, asignements in base R are always copying: library(data.table) df <- data.frame( t = 1:10, x = "x", y = "y" ) df$z <- df$y # I was assuming this is a full blown… Read More Is `<-` also for data.frames using call by reference sometimes?

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

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 far… Read More Split string and find unique characters for each row using data.table

Combine multiple rows based on ID column

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

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 a… 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"

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: NA,4,NA,1… Read More Split values of a data.table column and select the lowest number or NA if all are "NA"