Hi all, I have 2 data frames like the following: the first one df1: 'data.frame': 141obs. of 1 variable: $SerialNum: int 41006 41013 41044 41046 41067 41166 41202 41262 41274 41290 and the second one df2: 'data.frame': 194 obs. of 2 variables: $Serial: int 41006 41013 41018 41044 41046 41067 41111 41200 41262 41331 $Count : int 1 10 103 11 12 13 138 14 15 16 169 18 182 2 20 21 224 226 then I find the intersect of two data frames in serials which is: Matched=intersect(df1$SerialNum,df2$Serial) which gives me matched serials in both data frames. Next I want to get Count of these matched serials from df2 but I don't know how to make the right subset for this! Does anyone know how should I do that? Thanks for any help, Elahe
Hello, Elahe, look at ?match and check if df2$Count[ match( Matched, df2$Serial)] does what you want/need. Hth -- Gerrit --------------------------------------------------------------------- Dr. Gerrit Eichner Mathematical Institute, Room 212 gerrit.eichner at math.uni-giessen.de Justus-Liebig-University Giessen Tel: +49-(0)641-99-32104 Arndtstr. 2, 35392 Giessen, Germany Fax: +49-(0)641-99-32109 http://www.uni-giessen.de/eichner --------------------------------------------------------------------- Am 07.07.2016 um 13:34 schrieb ch.elahe via R-help:> Hi all, > I have 2 data frames like the following: > the first one df1: > > 'data.frame': 141obs. of 1 variable: > $SerialNum: int 41006 41013 41044 41046 41067 41166 41202 41262 41274 41290 > and the second one df2: > > 'data.frame': 194 obs. of 2 variables: > $Serial: int 41006 41013 41018 41044 41046 41067 41111 41200 41262 41331 > $Count : int 1 10 103 11 12 13 138 14 15 16 169 18 182 2 20 21 224 226 > then I find the intersect of two data frames in serials which is: > > Matched=intersect(df1$SerialNum,df2$Serial) > which gives me matched serials in both data frames. Next I want to get Count of these matched serials from df2 but I don't know how to make the right subset for this! Does anyone know how should I do that? > Thanks for any help, > Elahe > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
Hello, Maybe something like the following (untested). idx <- Matched %in% df2$Serial MatchedCount <- df2$Count[idx] Hope this helps, Rui Barradas ? Citando ch.elahe via R-help <r-help at r-project.org>:> Hi all, > I have 2 data frames like the following: > the first one df1: > > ? ?'data.frame':? 141obs. of 1 variable: > ? ?$SerialNum: int 41006 41013 41044 41046 41067 41166 41202 41262 > 41274 41290 > and the second one df2: > > ? ?'data.frame':? 194 obs. of 2 variables: > ? ?$Serial: int? ?41006 41013 41018 41044 41046 41067 41111 41200 41262 41331 > ? ?$Count : int? ?1 10 103 11 12 13 138 14 15 16 169 18 182 2 20 21? 224 226 > then I find the intersect of two data frames in serials which is: > > ? ?Matched=intersect(df1$SerialNum,df2$Serial) > which gives me matched serials in both data frames. Next I want to > get Count of these matched serials from df2 but I don't know how to > make the right subset for this! Does anyone know how should I do that? > Thanks for any help, > Elahe > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.htmland provide commented, > minimal, self-contained, reproducible code.? [[alternative HTML version deleted]]
Hi Elahe, Use subset and the %in% operator: subset(df2, Serial %in% Matched)[["Count "]] Best wishes, Ulrik On Thu, 7 Jul 2016 at 13:37 ch.elahe via R-help <r-help at r-project.org> wrote:> Hi all, > I have 2 data frames like the following: > the first one df1: > > 'data.frame': 141obs. of 1 variable: > $SerialNum: int 41006 41013 41044 41046 41067 41166 41202 41262 41274 > 41290 > and the second one df2: > > 'data.frame': 194 obs. of 2 variables: > $Serial: int 41006 41013 41018 41044 41046 41067 41111 41200 41262 > 41331 > $Count : int 1 10 103 11 12 13 138 14 15 16 169 18 182 2 20 21 224 > 226 > then I find the intersect of two data frames in serials which is: > > Matched=intersect(df1$SerialNum,df2$Serial) > which gives me matched serials in both data frames. Next I want to get > Count of these matched serials from df2 but I don't know how to make the > right subset for this! Does anyone know how should I do that? > Thanks for any help, > Elahe > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]