I have data frame that looks like this:
structure(list(type = c("TLAcmeth", "TLAcmeth", "TLAcmeth", "TLAcmeth",
"TLAcmeth", "TLAcmeth", "TLAcmeth", "TLAcmeth", "TLAmeandaily",
"TLAmeandaily", "TLAmeandaily", "TLAmeandaily", "TLAmeandaily",
"TLAmeandaily", "TLAmeandaily", "TLAmeandaily", "aerial", "aerial",
"aerial", "aerial", "aerial", "aerial", "aerial", "aerial"),
season = c("Fall", "Fall", "Spring", "Spring", "Summer",
"Summer", "Winter", "Winter", "Fall", "Fall", "Spring", "Spring",
"Summer", "Summer", "Winter", "Winter", "Fall", "Fall", "Spring",
"Spring", "Summer", "Summer", "Winter", "Winter"), daytype = c("Weekday",
"Weekend/Holiday", "Weekday", "Weekend/Holiday", "Weekday",
"Weekend/Holiday", "Weekday", "Weekend/Holiday", "Weekday",
"Weekend/Holiday", "Weekday", "Weekend/Holiday", "Weekday",
"Weekend/Holiday", "Weekday", "Weekend/Holiday", "Weekday",
"Weekend/Holiday", "Weekday", "Weekend/Holiday", "Weekday",
"Weekend/Holiday", "Weekday", "Weekend/Holiday"), SeasAV = c(2163.40757575758,
4545.83194444444, 2832.95277777778, 3763.33055555556, 3341.87121212121,
4754.50606060606, 1118.32121212121, 1030.36515151515, 1880.39449388995,
3900.1765284028, 2485.72769150217, 2913.10410947887, 2838.91832907833,
4194.02223304473, 968.473632205897, 1068.55954517396, 1661.23636363636,
2977.9375, 1653.83333333333, 4126.1375, 1912.48939393939,
2931.04848484848, 599.771212121212, 680.454545454545)), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -24L), groups = structure(list(
type = c("TLAcmeth", "TLAcmeth", "TLAcmeth", "TLAcmeth",
"TLAmeandaily", "TLAmeandaily", "TLAmeandaily", "TLAmeandaily",
"aerial", "aerial", "aerial", "aerial"), season = c("Fall",
"Spring", "Summer", "Winter", "Fall", "Spring", "Summer",
"Winter", "Fall", "Spring", "Summer", "Winter"), .rows = structure(list(
1:2, 3:4, 5:6, 7:8, 9:10, 11:12, 13:14, 15:16, 17:18,
19:20, 21:22, 23:24), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -12L), .drop = TRUE))
And one that looks like this:
structure(list(season = c("Fall", "Fall", "Spring", "Spring",
"Summer", "Summer", "Winter", "Winter"), daytype = c("Weekday",
"Weekend/Holiday", "Weekday", "Weekend/Holiday", "Weekday", "Weekend/Holiday",
"Weekday", "Weekend/Holiday"), n = c(61L, 30L, 65L, 27L, 65L,
27L, 61L, 29L)), class = c("grouped_df", "tbl_df", "tbl", "data.frame"
), row.names = c(NA, -8L), groups = structure(list(season = c("Fall",
"Fall", "Spring", "Spring", "Summer", "Summer", "Winter", "Winter"
), daytype = c("Weekday", "Weekend/Holiday", "Weekday", "Weekend/Holiday",
"Weekday", "Weekend/Holiday", "Weekday", "Weekend/Holiday"),
.rows = structure(list(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -8L), .drop = TRUE))
I would like to join the "n" column to first dataframe but matching it to each season/daytype. The issue here is I have three types of data so I need to state the "n" column three times for each type of data. Is there a way to do this?
Use merge
and then subset down to the columns to what you want to keep
df3 <- merge(df1, df2, by=c("season","daytype"))
df4 <- df3[,c('col1','col2','col3','col4')]