I have a dataframe of countries and connected data, but the countries are only listed by integer codes. I want to create a new variable for the country name and have a function change a given row's country name to the appropriate one.
I can easily do this for any given country with the following code:
for (i in 1:nrow(mena)){
if (mena$ccode[i] == 600){
mena$cname[i] <- "Morocco"}
However, when I try to make a function that takes country name and country code as its argument, my code doesn't result in an error message, but doesn't affect my dataframe at all. Here is the function I wrote:
assignGWName <- function(countryname, countrycode){
for (i in 1:nrow(mena)){
if (mena$ccode[i] == countrycode){
mena$cname[i] <- countryname}
}
}
If I call
assignGWName("Morocco", 600)
I get no effect. Why?