Answer for Calculating Population Change over Time with R


title: “Introduction to R Syntax”

output: html_notebook



calculate_annual_growth <- function(year_one,year_two,pop_y1, pop_y2,city) {

annual_growth <- (((pop_y2 - pop_y1) / pop_y1) * 100) / (year_two-year_one)

message <- paste("From", year_one, "to", year_two, "the population of", city, "grew by approximately", annual_growth, "% each year.")

print(message)

return(annual_growth)

}

# Write your code starting here:

city_name <- "Istanbul, Turkey"

pop_year_one <- 691000

pop_year_two <- 983000

#pop_year_three <- 8831800

pop_year_four <- 15029231

pop_change <- pop_year_four - pop_year_one

percent_gr <- ((pop_year_one - pop_year_four)/pop_year_four) * 100

annual_gr <- percent_gr - pop_change

print(annual_gr)

Hello! Did you have a question about this code, or did you want feedback? Unfortunately, one isn’t allowed to just post working code without some context.

1 Like

the formula is " percentage_gr < - (pop_present - pop_past) / pop_past * 100" so the line 13 of your code is wrong I guess?

and for the line 15, it’s divison not substraction, cuz the request is to produce the annual change rate as a result.