Section 6 Loops in R
In this brief section, we will go over conducting loops in R.
Loops are a tool in R that are useful for situations where we want to do something over and over and over and … over again, where we just change something small each time.
A quick example using the Basque data from the previous section:
basque <- read.csv("basque.csv", stringsAsFactors = T)Let’s say I wanted to know the GDP for each region for the earliest year they are in the data.
regionsubset <- subset(basque, region == "Andalucia")
regionsubset$gdpcap[regionsubset$year == min(regionsubset$year)]## [1] 1.688732
## Repeat for a new region
regionsubset <- subset(basque, region == "Aragon")
regionsubset$gdpcap[regionsubset$year == min(regionsubset$year)]## [1] 2.288775
## Repeat for a new region
regionsubset <- subset(basque, region == "Baleares")
regionsubset$gdpcap[regionsubset$year == min(regionsubset$year)]## [1] 3.143959
Ughhh can we automate this? we have 17 regions!!!
unique(basque$region)## [1] Andalucia Aragon Principado De Asturias
## [4] Baleares Canarias Cantabria
## [7] Castilla Y Leon Castilla-La Mancha Cataluna
## [10] Comunidad Valenciana Extremadura Galicia
## [13] Madrid Murcia Navarra
## [16] Basque Country Rioja
## 17 Levels: Andalucia Aragon Baleares Basque Country Canarias ... Rioja
Where we will be going by the end of this section:
gdpminyear <- rep(NA, 17) # empty "container" vector
regions <- unique(basque$region) # what we iterate through
names(gdpminyear) <- unique(basque$region) # labels for our output
for(i in 1:17){
regionsubset <- subset(basque, region == regions[i])
gdpminyear[i] <- regionsubset$gdpcap[regionsubset$year ==
min(regionsubset$year)]
}
head(gdpminyear) # output## Andalucia Aragon Principado De Asturias
## 1.688732 2.288775 2.502928
## Baleares Canarias Cantabria
## 3.143959 1.914382 2.559412
We got all of the answers with just one chunk of code!