making for loop question

Hello, i don't know how to make a for loop to show the population from 1952~2007
i made like that way. but it totally wrong.
can i get a hint or solution?

i<-year
> gapminder%>%for(i in 1952:2007) {
+     if(i==num(i)) {
+         print(sum(population=sum(pop)))
+ }
+ }
library(dplyr)
library(gapminder)

gapminder %>% 
  group_by(year) %>% 
  summarise(sum_pop = sum(as.numeric(pop), na.rm = TRUE))

If you really want to do this with a loop, here's a way:

library(gapminder)
library(tidyverse)

for (yeari in unique(gapminder$year)){
  gapminder %>% filter(year==yeari) %>%
    pull(pop) %>% sum() %>% print
}
#> [1] 2406957150
#> [1] 2664404580
#> [1] 2899782974
#> [1] 3217478384
#> [1] 3576977158
#> [1] 3930045807
#> [1] 4289436840
#> [1] 4691477418
#> [1] 5110710260
#> [1] 5515204472
#> [1] 5886977579
#> [1] 6251013179

Created on 2019-12-10 by the reprex package (v0.3.0)

You've been given the correct answers. Lets have a look at why yours doesn't work.

1] for does not take a data argument.
data %>% func(x,y,z) is equivalent to func(data, x, y, z). So what you wrote is equivalent to for (gapminder, i in 1952:2007) which makes no sense.

2] i==num(i) is a tautology. i is taken from a range of integers (1952:2007), so it is always an integer and will always match itself cast as double precision floating point number (numeric), regardless of what the actual number is.

3] your code does not subset the dataframe anywhere.

Maybe you should familiarize yourself more with basic R and get the step-by-step logic fundamentals clear in your head before rushing to do
more complex stuff with dplyr.

For future reference, you should include any warning and error messages your code causes when asking for help.

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.