### 1.1.1 PRACTICE: 

Using the 'countries' dataset, use a for loop to find the maximum value of GDP per capita (gdpPercap) for each country

```{r}
#answer:

for (i in unique(countries$country)){
  x <- max(countries$gdpPercap[countries$country == i])
  print(c(i, x))
}

```


### 1.1.2 PRACTICE: 

modify the code below to create a new dataframe with two columns: Party, and max midterms house change (x in the example code). Name your columns however you'd like. 

```{r}

#original code:
for (i in unique(midterms$party)){
  x <- max(midterms$house_change[midterms$party == i])
  print(x)
}

#answer: 
party <- 0
max_change <- 0
index <- 0

for (i in unique(midterms$party)){
  index <- index + 1 
  max_change[index] <- max(midterms$house_change[midterms$party == i])
  party[index] <- i
}

Max_Change_DF <- data.frame(party, max_change)
Max_Change_DF

```


## 2.1 PRACTICE

```{r}
#e.g., create a binary variable/column in the countries dataset called eurasia_bin, with values of 'Eurasia' and 'Elsewhere', based on this list (it is not totally accurate, but suitable for the purpose of practice): 

eurasia <- c("Europe", "Asia")

#(hint: you need to use %in%)

countries$eurasia_bin <- NA

for (i in 1:nrow(countries)){
if(countries$continent[i] %in% eurasia){
  countries$eurasia_bin[i] = 'Eurasia'} else {
    countries$eurasia_bin[i] = 'Elsewhere'
  }
}

countries
```

# 3. BONUS QUESTION

Loop through the storms dataset and calculate mean latitude for each storm. Store this in a new dataframe with storm names.
Then, create a new grouping variable for latitude, with three levels: 'North' (>30), 'Central' (>=20, <=30), 'South' (<20). 

```{r}
#answer: 
index <- 1 
mean_lat <- 0 
name <- 0 

for (i in unique(storms$name)){
  mean_lat[index]<-mean(storms$lat[storms$name == i])
  name[index] <- i
  index <- index + 1 
}

storms_df <- data.frame(name, mean_lat)

storms_df$location <- 0

for (i in 1:nrow(storms_df)){
  if (storms_df$mean_lat[i] < 30){
    storms_df$location[i] = 'South' 
    if (storms_df$mean_lat[i] > 20){
      storms_df$location[i] = 'Central' 
    }
  }
  else{ storms_df$location[i] = 'North'}
}

storms_df
```




