A solution with no dependencies looks like this::
Your data might look like this:
names <- data.frame(gender = c("Male", "Female", "Female"),
name = c("Sam", "Tina", "Anna"))
grades <- data.frame(class = c("A", "B", "A", "B", "C", "A"),
name = c("Sam", "Sam", "Tina", "Tina", "Tina", "Anna"),
grades = c(5, 5, 5, 5, 5, 5))
Then you can match the gender variable into the grades data.frame and summarize the grades by class and gender:
## Match the gender variable into your dataframe with grades
grades$gender <- names$gender[match(grades$name, names$name)]
## summarize grades by class and gender
result <- tapply(grades$grades, list(grades$class, grades$gender), sum)
Finally you will want to reshape to long and cleanup the data.frame a little:
## reshape to long and cleanup
result <- as.data.frame(result)
result$class <- rownames(result)
result <- reshape(result,
direction = "long",
varying = list(1:2),
timevar = "gender",
v.names = "grades")
result <- result[, c("class", "gender", "grades")]
You should get this:
> result
class gender grades
1.1 A 1 10
2.1 B 1 5
3.1 C 1 5
1.2 A 2 5
2.2 B 2 5
3.2 C 2 NA