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.
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.
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.
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.
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.
tapply(team.weight, list(team$team.jersey, team$team.sex), mean)
# female male
# blue 120 125
# green 160 NA
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With