-2

I am new to this forum :)

Here is the first row year | boys | girls | total | prop of boys :-------- :-------- : -------- : -------- 1 |1940 | 1211684 | 1148715 |2360399 | ?

I want to calculate the proportion of boys out of the total for each row and plot the boy's proportion overtime. secondly, I also want to calculate boy to girl ratio each year. How to do this. I am quite new to r, and I am not able to find the commands for this basic calculations.I am sure that many people here in this wonderful place can have an answer to my question. Any help will be greatly appreciated....Thank you very much

  • Hi user14864236, please type `dput(` into your console to and then paste the results (instead of copying and pasting the output of printing the variable). This will make it easier for others to reproduce your problem. – Mario Niepel Dec 21 '20 at 10:20
  • 1
    Hi. To get the desired help please provide a minimal reproducible example . – TarJae Dec 21 '20 at 10:21
  • There are many similiar questiosn like this, e.g. https://stackoverflow.com/questions/1660124/how-to-sum-a-variable-by-group – Max M Dec 21 '20 at 10:21
  • 1
    Hi Mario Niepel and TarJae, I have tried to follow them, but without success. I will try to learn those when I post future questions. Thank you – user14864236 Dec 21 '20 at 11:13

1 Answers1

0

Firstly I generate your data in R as a dataframe:

#set up data
data_df = data.frame(
  year = 1940,
  boys = 1211684,
  girls = 1148715,
  total = 2360399)

Then you calculate the columns you want to have. In R, many operations are vectorized, so this commands will work non-regarding how many rows of data you have.

#calculate proportions
data_df$proportion_boys <- data_df$boys / data_df$total
data_df$proportion_gender <- data_df$boys / data_df$girls

Now, we can plot the results. Note that R has basic plotting ability, but many people use ggplot2 for advanced plots. Either works for you.

#plot using base r
plot(data_df$proportion_boys ~ data_df$year)


#plot using ggplot2
library(ggplot2)
ggplot(data = data_df) + geom_point(aes(x = year, y = proportion_boys))
tlhenvironment
  • 319
  • 2
  • 10
  • Thank you so much tlhenvironment!!!I have solved it with your help. I have spent around two to three hours to find a solution to my problem. Now it is solved!!! BTW, I have already created a data frame before, but I forgot to mention. – user14864236 Dec 21 '20 at 10:57
  • Good luck on the journey <3 – tlhenvironment Dec 21 '20 at 11:06