kuropon kuropon - 5 months ago 33
R Question

R Row selection vector from data frame

I want to implement a simple row selection vector using the 'country' column on the below lat_all data frame in R.

pop country lat
12.4 Afghanistan 62
24.3 Australia 12
4 New Zealand 10
100 Japan 30
12 Fiji 28

Using a selection below which I can change easily such as

selection <- c("Afghanistan","Australia","Japan")

I want the result to be:

pop country lat
1 12.4 Afghanistan 62
2 24.3 Australia 12
4 100 Japan 30

Only way I can get what I want so far is using the below which doesn't use the selection vector

lat_all[ lat_all[2]==("Afghanistan") | lat_all[2]==("Australia") | lat_all[2]==("Japan"), ]

I don't understand why something like
doesn't work for me. The selection vector only finds Afghanistan and Japan. Australia is FALSE and any output using that vector skips Australia.

use subset function

lat_all  <- subset(lat_all, country %in% selection)