I've been beating my head against a wall on this one all night. I've got multiple data frames, each with the same general column structure. For each data frame, the columns carry a unique "_suffix". There are matching rows across the data frames. Here is an example:
cat_list <- c("mpg","spd","wt","price")
car <- c("bmw","mbz","lexus","bmw","mbz","lexus")
src <- c("usa","usa","usa","gb","gb","gb")
mpg_usa <- c(5,11,34,0,0,0)
mpg_gb <- c(0,0,0,44,12,9)
spd_usa <- c(55,13,7,0,0,0)
spd_gb <- c(0,0,0,15,43,67)
df <- data.frame(car, src, mpg_usa, mpg_gb, spd_usa, spd_gb)
I'd like to calculate the average for the matched rows based on a list of columns I specify. For example, my list of candidate categories is cat_list
. The data frame contains mpg
and spd
. I'd like to calculate the mean speed and mpg individually for the three car types across GB and the USA.
I've tried a few different version of melt
with no success. I figured out how to search for my column names using this:
avail.cats <- names(df)[grepl(paste(cat_list, collapse = "|"), names(df))]
However, that obviously doesn't help me collapse for calculation purposes.