user12059340
user12059340

Reputation:

How to rewrite two lines into one?

How to write these two lines as a one-liner?

which.max(WHO$Under15) ## output is 124
WHO$Country[124] ## output is "Niger"

When I enter which.max(WHO$Under15) I get the country number, which is 124. I then enter the country number in WHO$Country[] to get the country, which is Niger. I am wondering how to simplify this code to one line.

Upvotes: 0

Views: 82

Answers (2)

moodymudskipper
moodymudskipper

Reputation: 47320

You can also do :

subset(WHO, Under15 == max(Under15), "Country",drop = TRUE)

Or if you're using data.table

WHO[which.max(Under15), Country]

Upvotes: 0

Ben Bolker
Ben Bolker

Reputation: 226182

I think

WHO$Country[which.max(WHO$Under15)]

will do what you want, or

with(WHO,Country[which.max(Under15)])

Upvotes: 4

Related Questions