0

I have the data below. I would a sample that contains specific rows and all columns.The data contains information of over 15 countries. However, I would like to have the data for “South Africa” , “Tunisia” ,“Zambia”, and “Zimbabwe only. It means that I would like to have only these rows and their corresponding columns. How do I do that?

Case    CC3 Country Year    Banking Crisis  Banking_Crisis_Notes
4   AUS Australia   1800    0   
4   AUS Australia   1801    0   
4   AUS Australia   1802    0   
4   AUS Australia   1803    0   
NelsonGon
  • 13,015
  • 7
  • 27
  • 57

2 Answers2

1
exdf <- data.frame(country =rep(LETTERS[1:4], each = 4),
                   value = 1:16)
scountry <- c("A", "C")

# Using indexing
> exdf[exdf$country %in% scountry, ]
   country value
1        A     1
2        A     2
3        A     3
4        A     4
9        C     9
10       C    10
11       C    11
12       C    12

# Using subset function
> subset(exdf, country %in% scountry)
   country value
1        A     1
2        A     2
3        A     3
4        A     4
9        C     9
10       C    10
11       C    11
12       C    12
0

Here is the solution:

sample <- data[grep("South Africa|Tunisia|Zambia|Zimbabwe", data$Country), ]