I have two data frames with one same column name subject
. However, in the first data frame, there are many subeject 1 rows, but in the second data frame, there is only one subject 1 row. How can I combine these two data frames? Put it in other ways, how do I make a new column in the first data frame with values in the second data frames that match the subject number in the first data frame?
data frame 1
data1 <- structure(list( subject = c("subject1", "subject1", "subject1", "subject1",
"subject1", "subject1", "subject1", "subject1", "subject1", "subject1",
"subject1", "subject1", "subject1", "subject1", "subject10", "subject10",
"subject10", "subject10", "subject10", "subject10", "subject10", "subject10",
"subject10", "subject10", "subject10", "subject10", "subject10", "subject10"
)), row.names = c(NA, -28L), groups = structure(list( .rows = structure(list(
c(7L, 9L, 11L, 15L, 18L, 19L, 21L, 25L, 27L, 28L), 4:5, 1L,
c(3L, 14L, 17L, 26L), c(8L, 22L, 24L), c(6L, 10L, 12L, 23L
), c(2L, 13L, 16L, 20L)), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = c(NA, 7L), class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))
data frame 2
data2 <- structure(list(subject = c("subject1",
"subject10"), mean_per = c(63.5, 51.9285714285714)), row.names = c(NA,
-2L), groups = structure(list(subject = c("subject1", "subject10"
), .rows = structure(list(1L, 2L), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = 1:2, class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))