For loop: replacement has one row more than data

Viewed 13573

I am an beginner in R and have a problem. Any help would really be appreciated! When I apply the for loop in the following (simplified) case, I get an error message saying "replacement has 5 rows, data has 4"

Country <- c("Germany", "France", "Italy", "Spain")
Unemploy <- c(2, 3, 4, 10)
Growth <- c(2, 7, 6, 9)
data <- data.frame(Country, Unemploy, Growth)

for (i in data$Country) {
     if (identical(data$Country[i], "France")) {
          data$Growth[i] <- "5"
     } else {
          data$Growth[i] <- "2"
     }
}

Following message given out:

Error in `$<-.data.frame`(`*tmp*`, "Growth", value = c("2", "2", "2",  : 
replacement has 5 rows, data has 4
3 Answers
Related