Reputation: 155
I have two dataframes that look like this:
onlinedf :
month index Jevons
201408 1.0000000 1.0000000
201409 0.9881163 0.9881163
201410 0.9685963 0.9353384
201411 0.9772775 0.8951670
201412 0.9570516 0.7550252
201501 0.9614457 0.7520039
201502 0.9888529 0.7204573
201503 0.8893120 0.7124263
offlinedf:
month index Jevons
201408 1.0000000 1.0000000
201409 0.9881163 0.9881163
201410 0.9685963 0.9353384
201411 0.9772775 0.8951670
201412 0.9570516 0.7550252
201501 0.9614457 0.7520039
201502 0.9888529 0.7204573
201503 0.8893120 0.7124263
I've populated both dataframes with the same numbers for ease, but in practice the numbers within the dataframes are different and each dataframe has a different time period for which I have data.
I have weights for online and offline Jevons, e.g. the weight for 2014 online Jevons is 23.2 (therefore offline weight would be 76.8) and the weight for 2015 online Jevons is 25.6 (therefore offline weight would be 74.5).
What I want to do is be able to multiply all the online 2014 Jevons data by 23.2 and all the online 2015 Jevons data by 25.6. & then in the offline dataframe all the 2014 data by 76.8 and all 2015 data by 74.5.
Ideally my output would look something like this for the online data...
month index Jevons weightedJevons
201408 1.0000000 1.0000000 0.2320000
201409 0.9881163 0.9881163 0.2292430
201410 0.9685963 0.9353384 0.2169985
201411 0.9772775 0.8951670 0.2076787
201412 0.9570516 0.7550252 0.1751658
201501 0.9614457 0.7520039 0.1925130
201502 0.9888529 0.7204573 0.1844371
201503 0.8893120 0.7124263 0.1823811
I used a mutate function initially, but this only allowed me to apply one years weight and not change it for 2015. So then I tried to create my own function which failed miserably...
I started with something like this...
onlineweightFun <- function(x, y){
if(x starts_with(2014)){
y * 0.232
}
if(x starts_with(2015)){
y * 0.256
}
}
& tried variations of this until I decided to ask you guys on here. Any help would be very much appreciated! You can easily emulate my dataframe using:
month <- c("201408", "201409", "201410", "201411", "201412", "201501", "201502", "201503")
index <- c(1.0000000, 0.9881163, 0.9685963, 0.9772775, 0.9570516, 0.9614457, 0.9888529, 0.8893120)
Jevons <- c(1.0000000, 0.9881163, 0.9353384, 0.8951670, 0.7550252, 0.7520039, 0.7204573, 0.7124263)
onlinedf <- data.frame(month, index, Jevons)
Upvotes: 1
Views: 176
Reputation: 1563
Why not just:
month <- c("201408", "201409", "201410", "201411", "201412", "201501", "201502", "201503")
index <- c(1.0000000, 0.9881163, 0.9685963, 0.9772775, 0.9570516, 0.9614457, 0.9888529, 0.8893120)
Jevons <- c(1.0000000, 0.9881163, 0.9353384, 0.8951670, 0.7550252, 0.7520039, 0.7204573, 0.7124263)
onlinedf <- data.frame(month, index, Jevons, stringsAsFactors = FALSE)
result <- ifelse(startsWith(onlinedf$month, "2014"), onlinedf$Jevons * 0.232, onlinedf$Jevons * 0.256)
Upvotes: 0
Reputation: 39154
We can use substring
to extract the year information, and then based on the year information using case_when
to calculate the weights. onlinedf2
is the final output.
library(dplyr)
onlinedf2 <- onlinedf %>%
mutate(Year = substring(month, 1, 4)) %>%
mutate(weightedJevons = case_when(
Year == "2014" ~ Jevons * 0.232,
Year == "2015" ~ Jevons * 0.256,
TRUE ~ NA_real_
)) %>%
select(-Year)
onlinedf2
# month index Jevons weightedJevons
# 1 201408 1.0000000 1.0000000 0.2320000
# 2 201409 0.9881163 0.9881163 0.2292430
# 3 201410 0.9685963 0.9353384 0.2169985
# 4 201411 0.9772775 0.8951670 0.2076787
# 5 201412 0.9570516 0.7550252 0.1751658
# 6 201501 0.9614457 0.7520039 0.1925130
# 7 201502 0.9888529 0.7204573 0.1844371
# 8 201503 0.8893120 0.7124263 0.1823811
Upvotes: 3