This is very basic subsetting, and you can find several answers on SO and in any introductory manual.
Assuming you have read your csv file in as an object named "df", something like this should do the job:
df[df$country %in% c("United States", "Albania"), ]
In the future:
- Screenshots of your data are of little use. Please use something like
dput(head(yourdata))
instead.
- Show what you have tried. Don't simply write "I've been toying with the subset function". If you want to use the
subset
function in particular but haven't had success, it is helpful to show what you have done to help others troubleshoot.
A minimal example
Sample data:
set.seed(1)
df <- data.frame(country = sample(letters[1:5], 15, replace = TRUE),
somerandomvalue = rnorm(15),
anotherrandomvalue = rnorm(15))
Some summary data about the "country" column. Shows us that there are five unique countries, and there are 15 cases (rows) overall.
> summary(df$country)
a b c d e
2 5 1 4 3
Take just a subset:
> df[df$country %in% c("a", "b"), ]
country somerandomvalue anotherrandomvalue
1 b -0.005767173 0.80418951
2 b 2.404653389 -0.05710677
5 b -1.147657009 -0.69095384
10 a -0.891921127 -0.43331032
11 b 0.435683299 -0.64947165
12 a -1.237538422 0.72675075
14 b 0.377395646 0.99216037
Or, using the subset function:
subset(df, country %in% c("a", "b"))