r - How to recognize a category from a matrix -
i have matrix 6*4 matrix , stacked them 2 columns matrix. first column numerical values, , second column category names. want calculate mean of values each category don't know how characters column 2.
to aggregate values in data frame category, use aggregate
function. consider artificial data:
x <- data.frame(values=sample(1:6), categories=sample(c('a','b'), 6, replace=true)) x ## values categories ## 1 4 b ## 2 1 b ## 3 5 ## 4 3 b ## 5 6 ## 6 2 aggregate(values~categories, data=x, fun=mean) ## categories values ## 1 4.333333 ## 2 b 2.666667
Comments
Post a Comment