I have two datasets from the Global Energy Monitor and want to merge them. Both datasets show the “TrackerID” of different power plants. Dataset1 lists every TrackerID 1, 2, 3, ..., i. In Dataset2, some TrackerIDs are missing, while others are used several times. Therefore, one TrackerID in Dataset2 can have different values of the variable "signatory".
I want to create a weighted signatory variable calculating the weighted average of the different signatory status given in Dataset2 and include it in Dataset1. If TrackerID does not exists in Dataset2, set NA. If it does, calculate the weighted average.
I started to build a loop but it doesn’t work. This is what I’ve got so far, but maybe I’m on the wrong track:
allIDs <- as.list(Dataset1$TrackerID)
k = 1
for(i in allIDs){
Dataset1$weightedsignatory[k] <- i
k <- k + 1
}
This is a sample dataset to better understand my question:
Dataset1 <- data.frame(TrackerID = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
Name = c("a", "b", "c", "d", "e", "f", "g", "h", "i", "j"))
Dataset2 <- data.frame(TrackerID = c(1, 3, 3, 6, 7, 7, 7, 10),
Signatory= c(1, 1, 0, 0, 1, 0, 1, 1))
This is what the Dataset created by the loop should look like:
Dataset.wished <- data.frame(TrackerID = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
Name = c("a", "b", "c", "d", "e", "f", "g", "h", "i", "j"),
w.Signatory = c(1, "NA", 0.5, "NA", "NA", 0, 0.66, "NA", "NA", 1))
Any help is highly appreciated!