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
Use ifelse
instead
data[ ,"Growth"] <- ifelse(data[ , "Country"] == "France", "5", "2")
Check this out:
> for (i in data$Country){print(i)}
[1] "Germany"
[1] "France"
[1] "Italy"
[1] "Spain"
The i in data$Country
syntax iterates through the values in that data.frame
attribute. You are then using i
as if it is a numerical index. So what you are trying to do is something like this:
for (i in 1:length(data$Country)) {if (identical(data$Country[i],"France"))
+ {data$Growth[i]<-"5"}else{data$Growth[i]<-"2"}}
That being said the above is not idiomatic R, please see @Jilber's answer for a more idiomatic solution.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With