0

This a simplified display of my data:

enter image description here

I am interested in how Germany rankings on each of the variables, population, area, Literacy rate_male, etc.

How can I do it?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • 1
    Please provide your data as text so that we can copy & paste to use it. You can use `dput()` to do that. – F. Privé Aug 25 '18 at 12:19
  • 1
    It would be better if you give a reproducible example. Please read [How to make a great R reproducible example?](https://stackoverflow.com/q/5963269/4996248) – John Coleman Aug 25 '18 at 12:19
  • *"I am interested in how Germany rankings on each of the variables, population, area, Literacy rate_male, etc."* I don't understand what that means. Do you want to rank values in `Population`, `Area`, etc. and then determine the rank of `location_name = Germany`? – Maurits Evers Aug 25 '18 at 12:56

1 Answers1

1

You seem to have a dataframe with one variable the name of a country and the others numeric. To get an easily reproducible example with a similar structure: start with the built-in dataframe mtcars (evaluate ?mtcars for details) and then run the following two commands:

mtcars$make <- row.names(mtcars)
row.names(mtcars) <- NULL

Say you wanted to get the ranks of Datsun 710 (the third row of the dataframe). Then you could use:

ranks <- apply(mtcars[,names(mtcars) != "make"],2,function(v) rank(v)[which(mtcars$make == "Datsun 710")])

Resulting variable looks like:

> ranks
 mpg  cyl disp   hp drat   wt qsec   vs   am gear carb 
24.5  6.0  6.0  7.0 20.0  7.0 23.0 25.5 26.0 21.5  4.0 

In your case, something along the lines of

ranks <- apply(df[,names(df) != "location_name"],2,function(v) rank(v)[which(df$location_name == "Germany")])

should work.

John Coleman
  • 51,337
  • 7
  • 54
  • 119