How to merge/compare several tables by complex terms in R -
i tried compare 2 tables 2 terms. code in r:
cbr <- c("usd", "eur", "sek", "rur") value <- c(2, 4, 5, 9) data <- data.frame(cur = cbr, rate = value) deals1 <- c("usd", "usd", "usd", "eur", "sek", "eur", "rur", "sek") deals2 <- c("rur", "rur", "eur", "rur", "rur", "usd", "usd", "rur") day <- 1:8 dealsn <- data.frame(day = day, cur_1 = deals1, cur_2 = deals2) (i in 1:nrow(dealsn)) { (j in 1:nrow(data)) { if (dealsn$cur_1[i] == data$cur[j] & dealsn$cur_2[i] == "rur") { dealsn$data[i] <- data$rate[data$cur[j]] } else if (dealsn$cur_1[i] == data$cur[j] & dealsn$cur_2[i] != "rur") { dealsn$data[i] <- data$rate[data$cur[dealsn$cur_2[i]]] /data$rate[data$cur[j]] } } } dealsn
but not , result wrong:
day cur_1 cur_2 data 1 1 usd rur 9.00 2 2 usd rur 9.00 3 3 usd eur 1.00 4 4 eur rur 2.00 5 5 sek rur 5.00 6 6 eur usd 2.50 7 7 rur usd 1.25 8 8 sek rur 5.00
it should this, hope:
day cur_1 cur_2 data 1 usd rur 2 2 usd rur 2 3 usd eur 2 4 eur rur 4 5 sek rur 5 6 eur usd 0,5 7 rur usd 0,22 8 sek rur 5
help me please!
Comments
Post a Comment