I have a matrix a 6*4 matrix and stacked them into a two columns matrix. First column is the numerical values, and second column is the category names. I want to calculate the mean of the values from each category but I don't know how to get the characters from column 2.
            Asked
            
        
        
            Active
            
        
            Viewed 68 times
        
    1
            
            
        - 
                    Do you mean that you have a two-column data frame? – Matthew Lundberg Jun 08 '14 at 01:48
- 
                    Welcome to Stack Overflow. It's always best to include a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with your question. Show some sample data and your expected output. This helps to make it clear exactly what you want. – MrFlick Jun 08 '14 at 01:52
1 Answers
1
            To aggregate values in a data frame by category, you use the aggregate function.  Consider this 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          A
## 4      3          B
## 5      6          A
## 6      2          A
aggregate(values~categories, data=x, FUN=mean)
##   categories   values
## 1          A 4.333333
## 2          B 2.666667
 
    
    
        Matthew Lundberg
        
- 42,009
- 6
- 90
- 112
 
    