[R] Identyfing rows with specific conditions revisited

Ek Esawi esawiek at gmail.com
Tue Aug 15 00:38:54 CEST 2017


Allaisone 1 allaisone1 at hotmail.com
Mon May 22 02:10:10 CEST 2017
Hi All—

I am curious as to whether there is a vectorized solution using base R
functions, instead of looping and if statements, to the problem below. I
have seen several posts that address a similar question which generally ask
to count, identify, etc. a set of values in a row in a data frame or matrix
in a much bigger data frame that may have more or less columns compared to
the smaller one.

The post below is a very good example of what I am asking for.

The original post is at https://stat.ethz.ch/pipermail
/r-help/2017-May/447055.html
David Carlson provided an excellent solution via looping and conditional
statements. He mentioned that for such as big problem (the first data frame
has 2000 and the 2nd has 300,000 rows), looping may not be the most
efficient.

The original question was: I would like to identify all customer’s ids who
have had each meal combination in the first table so the final output would
be the first table with ids attached next to each meal combination in each
row like this:
  mealAcode  mealBcode  ids

1     34        39            11

2     25       34              15   11

3      25     77                15   85


M <- structure(list(mealAcode = c(34L, 89L, 25L, 34L, 25L), mealBcode =
c(66L, 39L, 77L, 39L, 34L)), .Names = c("mealAcode", "mealBcode"),
row.names = c(NA, -5L), class = "data.frame")
C <- structure(list(id = c(15L, 11L, 85L), M1 = c(77L, 25L, 89L), M2 =
c(34L, 34L, 25L), M3 = c(25L, 39L, 77L)), .Names = c("id", "M1", "M2",
"M3"), class = "data.frame", row.names = c(NA, -3L))

Thanks, in advance--EK

	[[alternative HTML version deleted]]



More information about the R-help mailing list