Someone please help me with for functions and histogram

I am doing an assignment where I need to create a histogram. That is no problem and I have been able to do this for the whole data set. I can not get it to do it for each year whatever I try.

(c) For every year from 2008 to 2011 plot a histogram as well as a bar graph of gdp per capita using a “for”-loop.

I know this isn't correct but this is my most recent attempt:
for (combined$year in 2008) {
hist(combined$GDP_fixprices_fixPPP/combined$pop,
main="Histogram of GDP per capita",
xlab="GDP per Capita",
border="blue",
col="green",
las=1,
breaks=5)
}

My dataset is called combined

Check the documentation for "for-loops", the general syntax is for (variable in vector), see this example.

for (i in unique(iris$Species)) {
    hist(iris[iris$Species == i,]$Sepal.Length,
         main= paste("Histogram of", i, "sepal length"),
         xlab="Sepal Length")
}

Created on 2019-11-23 by the reprex package (v0.3.0.9000)

2 Likes

You are a legend, i got it to work now. Thank you so much

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