rle

count unique combinations of variable values in an R dataframe column [duplicate]

爷,独闯天下 提交于 2020-12-08 07:08:41
问题 This question already has answers here : Collapse / concatenate / aggregate a column to a single comma separated string within each group (4 answers) Count number of rows within each group (15 answers) Closed 5 months ago . I want to count the unique combinations of a variable that appear per group. For example: df <- data.frame(id = c(1,1,1,2,2,2,3,3,4,4,4,5,6,6,7,7,7), status = c("a","b","c","a","b","c","b","c","b","c","d","b","b","c","b","c", "d")) > df id status 1 1 a 2 1 b 3 1 c 4 2 a 5

To check if a value in a row is repeated between groups in R

喜夏-厌秋 提交于 2020-12-06 06:06:11
问题 I have a dataset containing purchases made by different households across different retailers. For eg Example Dataset Using dput() structure(list(household_code = c(76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 76, 126, 126, 126, 126, 126, 126, 126, 126, 126), trip_code_uc = c(1032497498L, 1025776063L, 1029419047L, 1030418100L, 1029502602L, 1034153056L, 1027035051L, 1027533991L, 1033515804L, 1032998207L, 1032066227L, 1028192785L, 1033419039L, 1028730296L,