-1

Suppose there is a data frame like (call it df1)

203       Feb 2014  353
204       Feb 2015  416
205       Feb 2057    2
206       Feb 2058    1
207       Feb 2062    1
208       Feb 2064    1
209       Feb 2065    1
210       Feb 2066    4
211       Feb 2067   10
212       Feb 2068    3
213       Jan 1969  123
214       Jan 1970  120
215       Jan 1971  162
216       Jan 1972  159
217       Jan 1973  109
218       Jan 1974   98

and another dataframe like ( call it df2)

1         Feb 2014
2         Jan 1974

then how do i make a subset of the df1 such that

204       Feb 2015  416
218       Jan 1974   98

is there way of doing this with base R?

hrbrmstr
  • 77,368
  • 11
  • 139
  • 205
  • have a look at `?merge`, [and in particular, this question and answer](http://stackoverflow.com/questions/1299871/how-to-join-merge-data-frames-inner-outer-left-right) – tospig Oct 11 '15 at 22:05

2 Answers2

1

Assuming that your data looks like this:

df1 <- read.table(text='203       "Feb 2014"  353
                  204       "Feb 2015"  416
                  205       "Feb 2057"    2
                  206       "Feb 2058"    1
                  207       "Feb 2062"    1
                  208       "Feb 2064"    1
                  209       "Feb 2065"    1
                  210       "Feb 2066"    4
                  211       "Feb 2067"   10
                  212       "Feb 2068"    3
                  213       "Jan 1969"  123
                  214       "Jan 1970"  120
                  215       "Jan 1971"  162
                  216       "Jan 1972"  159
                  217       "Jan 1973"  109
                  218       "Jan 1974"   98')


df2 <- read.table(text='1         "Feb 2015"
2         "Jan 1974"')

And also assuming that you mean Feb 2015 and not Feb 2014 (because Feb 2015 has the other column as 416) you could do:

#you use the %in% operator to find which elements of df2 exist in df1
#and use those to subset df1
df1[df1$V2 %in% df2$V2, ]
    V1       V2  V3
2  204 Feb 2015 416
16 218 Jan 1974  98
LyzandeR
  • 37,047
  • 12
  • 77
  • 87
-1

Try dplyr

library(dplyr)

df2 %>% left_join(df1)
bramtayl
  • 4,004
  • 2
  • 11
  • 18