Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Table of vector's means by two factors

Tags:

r

I am learning R, and I promise you I have searched high and low for an answer to this. It is so simple, but for some reason I cannot figure it out for the life of me!

I have a dataframe containing one numeric vector and two factors:

team.weight <- c(150,160,120,100) # player's weight
team.jersey <- factor(c("blue", "green", "blue", "blue")) # player's jersey color
team.sex <- factor(c("male", "female", "female", "male")) # player's sex
team <- data.frame(team.jersey, team.sex, team.weight)

I want to display a table (I forget what it is called) that shows the average weight of all players, that is, mean(team.weight), for each combination of levels for the two factor tables.

I can do this manually, but there has to be a better way!

mean(team.weight[c(team.jersey[1],team.sex[1])])
mean(team.weight[c(team.jersey[1],team.sex[2])])
mean(team.weight[c(team.jersey[1],team.sex[3])])
mean(team.weight[c(team.jersey[1],team.sex[4])])

mean(team.weight[c(team.jersey[2],team.sex[1])])
mean(team.weight[c(team.jersey[2],team.sex[2])])
mean(team.weight[c(team.jersey[2],team.sex[3])])
mean(team.weight[c(team.jersey[2],team.sex[4])])

mean(team.weight[c(team.jersey[3],team.sex[1])])
mean(team.weight[c(team.jersey[3],team.sex[2])])
mean(team.weight[c(team.jersey[3],team.sex[3])])
mean(team.weight[c(team.jersey[3],team.sex[4])])

mean(team.weight[c(team.jersey[4],team.sex[1])])
mean(team.weight[c(team.jersey[4],team.sex[2])])
mean(team.weight[c(team.jersey[4],team.sex[3])])
mean(team.weight[c(team.jersey[4],team.sex[4])])

Any help would be greatly appreciated. I know the answer is dumb, but I cannot understand what it is.

like image 898
Ludger Lassen Avatar asked Aug 25 '13 14:08

Ludger Lassen


People also ask

What are factors in R programming?

Factors are the data objects which are used to categorize the data and store it as levels. They can store both strings and integers. They are useful in the columns which have a limited number of unique values. Like "Male, "Female" and True, False etc. They are useful in data analysis for statistical modeling.

How do you set factor levels in R?

One way to change the level order is to use factor() on the factor and specify the order directly. In this example, the function ordered() could be used instead of factor() . Another way to change the order is to use relevel() to make a particular level first in the list.

Is factor a data type in R?

R's basic data types are character, numeric, integer, complex, and logical. R's basic data structures include the vector, list, matrix, data frame, and factors.

How do you check if a column is a factor in R?

factor() Function. is. factor() function in R Language is used to check if the object passed to the function is a Factor or not. It returns a boolean value as output.


2 Answers

tapply(team.weight, list(team$team.jersey, team$team.sex), mean)
#       female male
# blue     120  125
# green    160   NA
like image 87
Julius Vainora Avatar answered Nov 10 '22 22:11

Julius Vainora


Here is a plyr example:

> library(plyr)
> ddply(team,.(team.jersey,team.sex),summarize,avgWeight=mean(team.weight))
  team.jersey team.sex avgWeight
1        blue   female       120
2        blue     male       125
3       green   female       160
like image 20
David Avatar answered Nov 10 '22 21:11

David