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!
source share