Читать книгу The Big R-Book - Philippe J. S. De Brouwer - Страница 103
Merging data frames
ОглавлениеMerging allows to extract the subset of two data-frames where a given set of columns match.
data_test.1 <- data.frame( Name = c(“Piotr”, “Pawel”,“Paula”,“Lisa”,“Laura”), Gender = c(“Male”, “Male”,“Female”, “Female”,“Female”), Score = c(78,88,92,89,84), Age = c(42,38,26,30,35) ) data_test.2 <- data.frame( Name = c(“Piotr”, “Pawel”,“notPaula”,“notLisa”,“Laura”), Gender = c(“Male”, “Male”,“Female”, “Female”,“Female”), Score = c(78,88,92,89,84), Age = c(42,38,26,30,135) ) data_test.merged <- merge(x=data_test.1,y=data_test.2, by.x=c(“Name”,“Age”),by.y=c(“Name”,“Age”)) # Only records that match in name and age are in the merged table: print(data_test.merged) ## Name Age Gender.x Score.x Gender.y Score.y ## 1 Pawel 38 Male 88 Male 88 ## 2 Piotr 42 Male 78 Male 78
merge()