'R - how to use group by function properly [duplicate]

I'm trying to do the average and correlation for some variables sorted gender. I don't think my group_by function is working, for some reason.

data(PSID1982, package ="AER" )

PSID1982 %>% 
  group_by(gender) %>% 
  summarise(avgeduc = mean(PSID1982$education), avgexper = mean(PSID1982$experience), avgwage= mean(PSID1982$wage),cor_wagvseduc = cor( x=PSID1982$wage, y= PSID1982$education))

The result is just the summary statistics of the entire group, not broken up into different genders.



Solution 1:[1]

Your syntax is correct but when you are using pipes and dplyr functions you do not need to call the column name using PSID1982$Column_Name. You just use the name of the column as follows:

PSID1982 %>% 
  group_by(gender) %>% 
  summarise(avgeduc = mean(education), 
            avgexper = mean(experience), 
            avgwage= mean(wage),
            cor_wagvseduc = cor( x=wage, y= education))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 lovalery