R - create a new column, where each element is a list of values

I started working with R data.table, and I'm trying to do the following: For simplicity, suppose I have a ArticleReadings list as follows:

UserID Time ArticleID Category NumOfReading 'aaa' 7:50 'x' 'sports' 1 'bbb' 5:05 'x' 'sports' 1 'aaa' 8:40 'y' 'politics' 2 'aaa' 10:00 'z' 'sports' 3 

In the end, I need a new column that will contain a list of all categories read by a specific user. In this example, the value for user "aaa" will be the vector of "policy", "sport", and for user "bbb" it will be a vector with one element: "sport". I need this type of column because later I would like to have some manipulations on it (for example, calculate the Mode / dominant category or display popular combinations of categories), so I decided to first get a unique vector for each user, then sort it. All my tests, in order to have functions such as a new column value, led to setting vector values ​​separately for each element, and not a vector as a column value .... for example, one of my tests:

 CategoriesList <- function(x){sort(unique(x))} DT[,':='(UniqueCats=CategoriesList(Category)),by=userID] 

Since I am new to data.table and user-defined functions in R, I assume that I am missing some critical point for transferring the result to the vector ... Any help would be appreciated!

+5
source share
1 answer

If we need a list column in a dataset, wrap it with list

 DT[, UniqueCats := list(list(sort(unique(Category)))) , by = UserID] str(DT) #Classes 'data.table' and 'data.frame': 4 obs. of 6 variables: # $ UserID : chr "aaa" "bbb" "aaa" "aaa" # $ Time : chr "7:50" "5:05" "8:40" "10:00" # $ ArticleID : chr "x" "x" "y" "z" # $ Category : chr "sports" "sports" "politics" "sports" # $ NumOfReading: int 1 1 2 3 # $ UniqueCats :List of 4 # ..$ : chr "politics" "sports" # ..$ : chr "sports" # ..$ : chr "politics" "sports" # ..$ : chr "politics" "sports" 

We can also create a row column by combining elements with paste

 DT[, uniqueCats := toString(sort(unique(Category))), by = UserID] 
+5
source

All Articles