1

I am using the aggregate function to show the mean of log(wage) by gender and race.

aggregate.data.frame(log_wage, list(race=race,gender=gender),  FUN = mean)

This is the output:

  race gender        x
1 black female 2.726742
2 other female 2.858445
3 white female 2.819883
4 black   male 2.864154
5 other   male 3.027230
6 white   male 3.065787

How can I show the values as table like this: enter image description here

sql runner
  • 25
  • 4

1 Answers1

0

This seems to answer your question:

DATA:

df <- data.frame(
  race = sample(c("black", "other", "white"), 100, replace = T),
  gender = sample(c("f", "m"), 100, replace = T),
  x = rnorm(100))

EDIT:

The most elegant and quickest way to achieve your goal is to compute the means not via aggregate but via tapply, thus:

tapply(df$x, list(df$race, df$gender), mean)

The output is pretty much what you want:

                f          m
black  0.13584749  0.1928834
other -0.37386003  0.2078025
white -0.09913409 -0.1672589

Other solution:

This computes the means by gender and race using aggregate:

df1 <- aggregate(
  df$x,
  list(df$race, df$gender),
  FUN = mean, na.rm = TRUE
)
df1
  Group.1 Group.2           x
1   black       f  0.13584749
2   other       f -0.37386003
3   white       f -0.09913409
4   black       m  0.19288337
5   other       m  0.20780250
6   white       m -0.16725892

To get more meaningful column names rename the columns:

colnames(df1) <- c("race", "gender", "x")

Change df1to wide format using reshape:

reshape(df1, idvar = "race", timevar = "gender", direction = "wide")
   race         x.f        x.m
1 black  0.13584749  0.1928834
2 other -0.37386003  0.2078025
3 white -0.09913409 -0.1672589
Chris Ruehlemann
  • 20,321
  • 4
  • 12
  • 34