Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201867

Revaluing many observations with a for loop in R

$
0
0

I have a data set where I am looking at longitudinal data for countries.

> head(master.set)
      Country  Country.ID Year Happiness.Score   GDP.PPP GINI Status
1 Afghanistan Afghanistan 2015           3.575  1766.593   NA      2
2 Afghanistan Afghanistan 2016           3.360  1757.023   NA      2
3 Afghanistan Afghanistan 2017           3.794  1758.466   NA      2
4     Albania     Albania 2015           4.959 10971.044   NA      2
5     Albania     Albania 2016           4.655 11356.717   NA      2
6     Albania     Albania 2017           4.644 11803.282   NA      2

I created that Country.ID variable with the intent of turning them into numerical values 1:159. I am hoping to avoid doing something like this to replace the value at each individual observation: master.set$Country.ID <- master.set$Country.ID[master.set$Country.ID == "Afghanistan"] <- 1

As I implied, there are 159 countries listed in the data set. Because it' longitudinal, there are 460 observations.

Is there any way to use a for loop to save me a lot of time? Here is what I attempted. I made a couple of lists and attempted to use an ifelse command to tell R to label each country the next number. Here is what I have:

#List of country names
N.Countries <- length(unique(master.set$Country))
Country <- unique(master.set$Country) 
Country.ID <- unique(master.set$Country.ID)
CountryList <- unique(master.set$Country)

#For Loop to make Country ID numerically match Country
for (i in 1:460){
  for (j in N.Countries){
    master.set[[Country.ID[i]]] <- ifelse(master.set[[Country[i]]] == CountryList[j], j, master.set$Country)
  }
}

I received this error:

Error in `[[<-.data.frame`(`*tmp*`, Country.ID[i], value = logical(0)) : 
  replacement has 0 rows, data has 460

Does anyone know how I can accomplish this task? Or will I be stuck using the ifelse command 159 times?

Thanks!


Viewing all articles
Browse latest Browse all 201867

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>