如果条件为真,则将矩阵中的值除以另一个矩阵

时间:2018-05-29 12:33:50

标签: r dataframe matrix elementwise-operations

我有三个带数值的数据框。它们彼此相关并且具有相同的列名但显示不同的内容。

dat1 = cbind(a = c(2,1,3),
                  b = c(2,2,3),
                  c = c(2,2,2))
     a b c
[1,] 2 2 2
[2,] 1 2 2
[3,] 3 3 2

dat2 = cbind(a = c(20, 20, 20),
                  b = c(30, 30, 30),
                  c = c(50, 50, 50))
     a  b  c
[1,] 20 30 50
[2,] 20 30 50
[3,] 20 30 50

dat3 = cbind("1" = c(100,100,100),
            "2" = c(200, 200, 200),
            "3" = c(300, 300, 300))

       1   2   3
[1,] 100 200 300
[2,] 100 200 300
[3,] 100 200 300

忽略dat2dat3中值的模式。这只是为了说明。我的数据集有更多随机值。

获取result

         a     b    c
[1,] 0.100 0.150 0.25
[2,] 0.200 0.150 0.25
[3,] 0.067 0.067 0.25

基本上是

     1        2        3       
[1,] "20/200" "30/200" "50/200"
[2,] "20/100" "30/200" "50/200"
[3,] "20/300" "30/300" "50/200"

理由是,如果dat1中的值的值为1,则从dat2中取相应的值,然后除以dat3中的相应值和列。同样,如果dat1中的值在2中的值为dat1,则从dat2获取相应的值(具有相同的“位置”)并除以相应的值dat3中的值和列。 3的值也是如此。

例如,让我们看看dat11的值。我们只有一个数据点的值为1,而dat2[2,1]的值为20。取20并除以dat3[2,1]。或者查看来自dat2[3,1]的值为3的{​​{1}}。取dat1(值为dat2[3,1])并在20中除以300,因为dat3中的值为3

有办法做到这一点吗?

以下是使用dat1分别为dput()dat1的数据输出。

dat3

以下是实际数据的维度。

structure(list(DNB = c(1, 1, 1), `NORSK HYDRO` = c(2, 2, 2), ORKLA = c(1, 1, 1), STOREBRAND = c(1, 1, 2), ATEA = c(1, 1, 1), `SCHIBSTED A` = c(2, 2, 2), BONHEUR = c(1, 1, 1), EKORNES = c(1, 1, 1), `KONGSBERG GRUPPEN` = c(2, 2, 2), `TOMRA SYSTEMS` = c(1, 1, 1), VEIDEKKE = c(1, 1, 2), `ARENDALS FOSSEKOMPANI` = c(2, 2, 2), `OLAV THON EIEP.` = c(2, 2, 2), `PETROLEUM GEO SERVICES` = c(2, 2, 2), `SPAREBANK 1 SR BANK` = c(2, 2, 2), `STOLT-NIELSEN` = c(2, 2, 2), `ODFJELL 'A'` = c(1, 1, 1), `SPAREBANK 1 NORD-NORGE` = c(2, 2, 2), `SPAREBANK 1 SMN` = c(2, 2, 2), `WILHS.WILHELMSEN HDG.'A'` = c(2, 2, 2), `NORDEA BANK (~NK)` = c(2, 2, 2), `ATLAS COPCO 'A' (~NK)` = c(2, 2, 2), `VOLVO 'B' (~NK)` = c(2, 2, 2), `SANDVIK (~NK)` = c(2, 2, 2), `SWEDBANK 'A' (~NK)` = c(1, 1, 1), `ERICSSON 'B' (~NK)` = c(1, 1, 1), `SVENSKA HANDBKN.'A' (~NK)` = c(1, 1, 1), `HENNES & MAURITZ 'B' (~NK)` = c(2, 2, 2), `SEB 'A' (~NK)` = c(2, 2, 2), `INVESTOR 'B' (~NK)` = c(1, 1, 1), `SWEDISH MATCH (~NK)` = c(1, 1, 1), `ELECTROLUX 'B' (~NK)` = c(2, 2, 2), `SKANSKA 'B' (~NK)` = c(2, 2, 1), `SCA 'B' (~NK)` = c(2, 2, 2), `SECURITAS 'B' (~NK)` = c(2, 2, 2), `HOLMEN 'B' (~NK)` = c(2, 2, 2), `SSAB 'A' (~NK)` = c(1, 1, 1), `ERICSSON 'A' (~NK)` = c(2, 2, 2), `INVESTOR 'A' (~NK)` = c(2, 2, 2), `VOLVO 'A' (~NK)` = c(2, 2, 2), `NOVO NORDISK 'B' (~NK)` = c(2, 2, 2), `DANSKE BANK (~NK)` = c(1, 1, 1), `COLOPLAST 'B' (~NK)` = c(2, 2, 3), `CARLSBERG 'B' (~NK)` = c(2, 2, 2), `A P MOLLER - MAERSK 'B' (~NK)` = c(2, 2, 2), `TDC (~NK)` = c(2, 2, 2), `TOPDANMARK (~NK)` = c(2, 2, 2), `WILLIAM DEMANT HLDG. (~NK)` = c(3, 3, 2), `JYSKE BANK (~NK)` = c(1, 1, 1), `KOBENHAVNS LUFTHAVNE (~NK)` = c(2, 2, 1), `NKT (~NK)` = c(1, 1, 1), `ROCKWOOL 'B' (~NK)` = c(2, 2, 2), `SYDBANK (~NK)` = c(2, 2, 2), `FLSMIDTH & CO.'B' (~NK)` = c(2, 2, 1), `GN STORE NORD (~NK)` = c(2, 2, 2), `ALK-ABELLO (~NK)` = c(2, 2, 2), `BANG & OLUFSEN 'B' (~NK)` = c(3, 3, 2), `SANTA FE GROUP (~NK)` = c(2, 2, 2), `CARLSBERG 'A' (~NK)` = c(2, 2, 2), `ROCKWOOL 'A' (~NK)` = c(2, 2, 2), `NOKIA (~NK)` = c(1, 1, 1), `SAMPO 'A' (~NK)` = c(1, 1, 1), `KONE 'B' (~NK)` = c(2, 2, 2), `UPM-KYMMENE (~NK)` = c(1, 1, 1), `WARTSILA (~NK)` = c(1, 1, 1), `METSO (~NK)` = c(1, 1, 1), `STORA ENSO 'R' (~NK)` = c(2, 2, 2), `HUHTAMAKI (~NK)` = c(1, 1, 1), `FINNAIR (~NK)` = c(2, 2, 2), `KEMIRA (~NK)` = c(1, 1, 1), `UPONOR (~NK)` = c(1, 1, 1), `KESKO 'B' (~NK)` = c(1, 1, 2), `ORION 'B' (~NK)` = c(2, 2, 2), `OUTOKUMPU 'A' (~NK)` = c(2, 2, 2), `RAISIO (~NK)` = c(2, 2, 2), `TIETO OYJ (~NK)` = c(1, 1, 1), `METSA BOARD 'B' (~NK)` = c(2, 2, 2), `ORION 'A' (~NK)` = c(2, 2, 2), `STOCKMANN 'A' (~NK)` = c(2, 2, 2), `STORA ENSO 'A' (~NK)` = c(2, 2, 2)), .Names = c("DNB", "NORSK HYDRO", "ORKLA", "STOREBRAND", "ATEA", "SCHIBSTED A", "BONHEUR", "EKORNES", "KONGSBERG GRUPPEN", "TOMRA SYSTEMS", "VEIDEKKE", "ARENDALS FOSSEKOMPANI", "OLAV THON EIEP.", "PETROLEUM GEO SERVICES", "SPAREBANK 1 SR BANK", "STOLT-NIELSEN", "ODFJELL 'A'", "SPAREBANK 1 NORD-NORGE", "SPAREBANK 1 SMN", "WILHS.WILHELMSEN HDG.'A'", "NORDEA BANK (~NK)", "ATLAS COPCO 'A' (~NK)", "VOLVO 'B' (~NK)", "SANDVIK (~NK)", "SWEDBANK 'A' (~NK)", "ERICSSON 'B' (~NK)", "SVENSKA HANDBKN.'A' (~NK)", "HENNES & MAURITZ 'B' (~NK)", "SEB 'A' (~NK)", "INVESTOR 'B' (~NK)", "SWEDISH MATCH (~NK)", "ELECTROLUX 'B' (~NK)", "SKANSKA 'B' (~NK)", "SCA 'B' (~NK)", "SECURITAS 'B' (~NK)", "HOLMEN 'B' (~NK)", "SSAB 'A' (~NK)", "ERICSSON 'A' (~NK)", "INVESTOR 'A' (~NK)", "VOLVO 'A' (~NK)", "NOVO NORDISK 'B' (~NK)", "DANSKE BANK (~NK)", "COLOPLAST 'B' (~NK)", "CARLSBERG 'B' (~NK)", "A P MOLLER - MAERSK 'B' (~NK)", "TDC (~NK)", "TOPDANMARK (~NK)", "WILLIAM DEMANT HLDG. (~NK)", "JYSKE BANK (~NK)", "KOBENHAVNS LUFTHAVNE (~NK)", "NKT (~NK)", "ROCKWOOL 'B' (~NK)", "SYDBANK (~NK)", "FLSMIDTH & CO.'B' (~NK)", "GN STORE NORD (~NK)", "ALK-ABELLO (~NK)", "BANG & OLUFSEN 'B' (~NK)", "SANTA FE GROUP (~NK)", "CARLSBERG 'A' (~NK)", "ROCKWOOL 'A' (~NK)", "NOKIA (~NK)", "SAMPO 'A' (~NK)", "KONE 'B' (~NK)", "UPM-KYMMENE (~NK)", "WARTSILA (~NK)", "METSO (~NK)", "STORA ENSO 'R' (~NK)", "HUHTAMAKI (~NK)", "FINNAIR (~NK)", "KEMIRA (~NK)", "UPONOR (~NK)", "KESKO 'B' (~NK)", "ORION 'B' (~NK)", "OUTOKUMPU 'A' (~NK)", "RAISIO (~NK)", "TIETO OYJ (~NK)", "METSA BOARD 'B' (~NK)", "ORION 'A' (~NK)", "STOCKMANN 'A' (~NK)", "STORA ENSO 'A' (~NK)"), row.names = c(NA, 3L), class = "data.frame") structure(c(572008.53, 617720.24, 654277.81, 686839.49, 736058.9, 714108.91, 8344.65, 9753.26, 5407.72), .Dim = c(3L, 3L), .Dimnames = list( c("1", "2", "3"), c("1", "2", "3"))) 使用dat1提供

dim()

[1] 252 80 使用dat2提供

dim()

[1] 252 80 使用dat3提供

dim()

1 个答案:

答案 0 :(得分:1)

你想找到这样的东西我相信:

你需要将dat1和dat2转换为矩阵而不是数据帧。

dat1 <- as.matrix(dat1) 
dat2 <- as.matrix(dat2)

dfx <- do.call('rbind', lapply(1:nrow(dat1),function(x)dat3[x,dat1[x,]]))

要接收你需要将dat2除以dfx的比率(因为没有提供dat2,我将其留给OP解决):

dat2/dfx #This should give you final answer

<强>输出

> dat2/dfx
              a    b    c
[1,] 0.10000000 0.15 0.25
[2,] 0.20000000 0.15 0.25
[3,] 0.06666667 0.10 0.25

示例输出

> do.call('rbind', lapply(1:nrow(dat1),function(x)dat3[x,c(dat1[x,])]))
            1        2        1        1
[1,] 572008.5 686839.5 572008.5 572008.5
[2,] 617720.2 736058.9 617720.2 617720.2
[3,] 654277.8 714108.9 654277.8 714108.9
            1        2        1        1
[1,] 572008.5 686839.5 572008.5 572008.5
[2,] 617720.2 736058.9 617720.2 617720.2
[3,] 654277.8 714108.9 654277.8 654277.8