sorting - Keep observations only overlapped id in two data sets in R? -
i have 2 data sets. suppose data , data b , 2 data sets have id variables. in 2 data sets, want remove ids not overlapped. in other words, want keep ids exist in 2 data sets.
it depends on whether want separately each data set or if merge both data sets 1 data set (or data frame). can try dplyr
.
library(dplyr) df1 <- data.frame(id = c("a", "b", "c"), var1 = c(1,2,3)) df2 <- data.frame(id = c("a", "c", "e"), var2 = c(4,5,6)) # matching ids in data frame 1 dplyr::semi_join(df1, df2, "id") > id var1 > 1 1 > 2 c 3 # matching ids in data frame 2 dplyr::semi_join(df2, df1, "id") > id var1 > 1 4 > 2 c 5 # merge column both data frames # matching id dplyr::inner_join(df1, df2, "id") > id var1 var2 > 1 1 4 > 2 c 3 5
Comments
Post a Comment