I have two different data frames in R. The first one, df1
, contains data for a bunch of cities, each associated with it's corresponding state. The second one contains data aggregated by state, but dividing this data into seral classes. Like this:
states1 <- c("a", "a", "a", "a", "a", "b", "b", "b", "b", "c", "c", "d", "d", "d")
cities <- c("A","B","C","D","E","F","G","H","I","J","K","L","M","N")
data1 <- c(123, 222, 444, 125, 687, 987, 556, 445, 444, 659, 798, 113, 325, 144)
df1 <- data.frame(states1, cities, data1)
#
states2 <- c("a","a","b","b","c","c","d","d")
classes <- c(1,2,1,2,1,2,1,2)
data2 <- c(65,21,44,25,37,87,58,47)
df2 <- data.frame(states2, classes, data2)
I want to multiply the data columns from the two data frames based on a certain criteria creating a third data frame. I want, for each city, to multiply its data to the data of its corresponding state, creating two columns, one for each class.
For example:
For cities A and B, which belong to state a, and K, belonging to sate c, I need to multiply their data by the data of their respective states, and do that for each of the classes 1 and 2. That is, I want to multiply the city's data by the two classes' data using the corresponding states as my matching criteria. like that, for ex.:
multA <- c(123*65, 123*21)
multB <- c(222*65, 222*21)
multK <- c(798*37, 789*87)
df3 <- data.frame(rbind(multA, multB, multK))
colnames(df3) <- c("class 1", "class 2")
But of course, I want to do that automatically and for every city. I've tried to use the which
function and the dplyr
package, but so far I haven't come up with a solution. Is there any way I can do that without with a package or built in function, without needing writing an explicit loop? Thanks!!