Home > Back-end >  Generating New Columns by Group But Getting Unknown Results from World Bank Data
Generating New Columns by Group But Getting Unknown Results from World Bank Data

Time:01-15

I am trying to use a simple dplyr command to group World Bank data and then add all of the country populations for a given year into a new column. No matter what I seem to do I can't get anything except a column of NAs. I've even tried base R aggregate() and nothing seems to work? Not entirely sure what is going on--I converted all of the World Bank data to numeric data but nothing is coming out the other side.

The function I'm using is very similar to this: How to create a new variable that is the sum of a column, by group, in R?

df %>%
  group_by(group) %>%
  mutate(sum = sum(variable)) %>%
  ungroup()

Thank you!

Edit: also, the code works on any other dataset, so I'm not sure what I did wrong.

CodePudding user response:

Have you got NA's in your variable column?

If so, try this to get the sum ignoring missing values:

df %>%
  group_by(group) %>%
  mutate(sum = sum(variable, na.rm=TRUE)) %>%
  ungroup()
  •  Tags:  
  • Related