0

I have a dataset that contains information about multiple countries. As I am trying to construct population weights, I want to calculate: country population (variable included in the dataset) / sample size for each country (different for each country)

For one specific country, I would first create a subset (e.g. italydata = subset(data, data$country == "Italy") and then divide country_population by nrow(italydata).

I am looking for a way to do this calculation for each country in the dataset. I have tried it with dplyr package, but I am uncertain what to write instead of nrow("x").

weight_by_economy <- data %>%
                     group_by(country) %>%
                     summarize(weight = country_population/nrow(x))

Thanks for your help!

ella
  • 35
  • 3

1 Answers1

0

Try

weight_by_economy <- data %>%
                 group_by(country) %>%
                 summarize(weight = country_population/n())

if this doesn't work, please clarify the question by providing a representative data object.

marvinschmitt
  • 346
  • 1
  • 7