sp1.col <- c("A","A","A","A","B","B","B","B","C","C","C","C")
sp1.val <- c(1:12)
sp2.col <- c("A","B","C","D","A","B","C","D","A","B","C","D")
sp2.val <- c(1:12)
df <- data.frame(sp1.col, sp1.val, sp2.col, sp2.val)
I want subset the data-frame such that sp1 and sp2 have identical species names, and all other rows are eliminated.
So the final dataframe should look like
sp1.col sp1.val sp2.col sp2.val
A 1 A 1
B 5 B 5
C 11 C 11
CodePudding user response:
> with(df,df[sp1.col==sp2.col,])
sp1.col sp1.val sp2.col sp2.val
1 A 1 A 1
6 B 6 B 6
11 C 11 C 11
CodePudding user response:
We can use subset
in base R
(no packages needed)
subset(df, sp1.col == sp2.col)
-output
sp1.col sp1.val sp2.col sp2.val
1 A 1 A 1
6 B 6 B 6
11 C 11 C 11
CodePudding user response:
library(dplyr)
df %>%
filter(sp1.col == sp2.col)