0

I have two data frames (d1 and d2), they both have the same column names. I have one column called label_name in both which is a character with species names. Data frame 1 (d1) has only my species of interest (A and B), while data frame 2 (d2) has other species that I am not interested about as well as the ones I need (A, B, C, D). I want to bind the rows from d2 into d1 ONLY when label_name = A and B.

I tried using the if function, filter, and merge. But none of them worked.

jpsmith
  • 11,023
  • 5
  • 15
  • 36
  • `rbind(d1, subset(d2, label_name %in% c("A", "B")))` – r2evans Apr 19 '23 at 15:32
  • 3
    Welcome to SO, Isabella Avo! Questions on SO (especially in R) do much better if they are reproducible and self-contained. By that I mean including attempted code (please be explicit about non-base packages), sample representative data (perhaps via `dput(head(x))` or building data programmatically (e.g., `data.frame(...)`), possibly stochastically), perhaps actual output (with verbatim errors/warnings) versus intended output. Refs: https://stackoverflow.com/q/5963269, [mcve], and https://stackoverflow.com/tags/r/info. (If my comment doesn't work for you, please [edit] your question. Thanks!) – r2evans Apr 19 '23 at 15:32
  • Thank you! the code works! I have never seen the %in% and i will look it up! – Avosandokan Apr 19 '23 at 16:43
  • Please provide enough code so others can better understand or reproduce the problem. – Community Apr 19 '23 at 17:30
  • @IsabellaAvo, see https://stackoverflow.com/q/15358006/3358272, https://stackoverflow.com/q/42637099/3358272 – r2evans Apr 19 '23 at 18:05

0 Answers0