-2

Code Rank// i/q// exam score// Gender

1 4 40 86 Male

2 11 65 88 Female

3 27 80 70 Male

4 53 80 61 Male

5 4 40 89 Male

6 22 70 60 Female

7 16 20 81 Female

8 21 55 75 Female

9 25 50 69 Female

10 18 40 82 Female

from the following list what would be r code to calculate mean and standard deviation of exam score for male and female in separate.

zx8754
  • 52,746
  • 12
  • 114
  • 209
joe
  • 1
  • 1
  • 1
  • 2

1 Answers1

2

You can use dplyr package to achieve this easily:

> data
# A tibble: 10 × 5
    code  rank    iq  score gender
   <int> <int> <int>  <dbl>  <chr>
1      1     4    40 86.298   Male
2      2    11    65 88.716 Female
3      3    27    80 70.178   Male
4      4    53    80 61.312   Male
5      5     4    40 89.522   Male
6      6    22    70 60.506 Female
7      7    16    20 81.462 Female
8      8    21    55 75.820 Female
9      9    25    50 69.372 Female
10    10    18    40 82.268 Female

Grouping this data on gender column using group_by() and summarizing using summarise() can give you your answer:

> data %>%
+   group_by(gender) %>%
+   summarise(avg_score = mean(score),
+             sd_score = sd(score))
# A tibble: 2 × 3
  gender avg_score sd_score
   <chr>     <dbl>    <dbl>
1 Female  76.35733 10.13981
2   Male  76.82750 13.36397

There are other ways to do this too, but learning a bit of dplyr to do these things might be helpful in long run.

For a detailed tutorial, read Transformation chapter in Hadley Wickham's book R for Data Science

Nirmal
  • 667
  • 5
  • 9