aggregate - aggregating categories in R -
hi i'm new r , i'm trying aggregate list , count total not sure how it.
mylist =c("a", "b", "a", "a", "b") i can create function loops through list , groups each category , counts them. i'm sure there must easier way group can category , number each category. 3 , b 2.
i tried using function below believe don't have proper syntax.
aggr <-aggregate(mylist, count) thanks in advance.
i'm guessing you're looking table , not aggregate:
mylist =c("a", "b", "a", "a", "b") table(mylist) # mylist # b # 3 2 tapply can handy here:
tapply(mylist, mylist, length) # b # 3 2 and, suppose "trick" aggregate in following way:
aggregate(ind ~ mylist, data.frame(mylist, ind = 1), length) # mylist ind # 1 3 # 2 b 2 if you're looking understand why well, aggregate takes data.frame input, , specify 1 or more columns aggregated grouped 1 or more other columns (or vectors in workspace of same length number of rows).
in example above, converted vector data.frame, adding dummy column values "1". used formula ind ~ mylist (where ~ sort of "is grouped by") , set aggregation function length (there no count in base r, though function can found in different packages).
Comments
Post a Comment