简体   繁体   English

标量乘以 r 中的两个数据帧

[英]scalar multipling two data.frames in r

I have two data.frames in r.THe first one is我在 r 中有两个 data.frames。第一个是

                cases      population  urbanisation  density temperature  h_dev_index
Austria         563.375758 10.7969091     63.07388  134.08690   13.011172   0.9898000
Belgium         109.400000 13.5885455     99.38933  443.52112   16.185297   0.9829455
Bulgaria          0.000000  5.5320606     85.84782   52.30011   20.825068   0.9669576
Croatia           2.000000  3.4548485     64.21382   60.46082   20.855372   0.9288667

THe second one is:第二个是:

Estimate Std.估计标准。 Error t value Pr(>|t|)误差 t 值 Pr(>|t|)

(Intercept)                        -1.916e+00  7.144e-01  -2.682  0.00785 ** 
population                         -7.327e-03  1.572e-03  -4.659 5.37e-06 ***
cases                               1.473e-03  1.544e-04   9.541  < 2e-16 ***
urbanisation                        3.798e-03  1.962e-03   1.936  0.05410 .  
density                             8.132e-05  2.512e-04   0.324  0.74641    
temperature                        -3.518e-02  8.641e-03  -4.071 6.43e-05 ***
h_dev_index                         1.842e+00  8.800e-01   2.093  0.03743 *  

I need to multiply the coeffient from the first column from second data.frame to the dataframe.THe result should be(for Austria):我需要将第二个 data.frame 中第一列的系数乘以 dataframe。结果应该是(对于奥地利):

-1.916e+00 + 563.375758*(-7.327e-03)+ 10.7969091*(1.473e-03)+ 63.07388*(3.798e-03)+134.08690*(8.132e-05)+     13.011172*(-3.518e-02)+   0.9898000*1.842e+00

How should I solve this problem?我应该如何解决这个问题?

Try reshaping and merging:尝试重塑和合并:

library(tidyverse)
#Code
res <- df1 %>% rownames_to_column('id') %>%
  pivot_longer(-id) %>%
  left_join(df2 %>% select(1,2) %>% rename(name=V1)) %>%
  group_by(id) %>%
  summarise(Val=sum(value*V2)+df2$V2[df2$V1=='(Intercept)'])

Output: Output:

# A tibble: 4 x 2
  id          Val
  <chr>     <dbl>
1 Austria   0.451
2 Belgium  -0.200
3 Bulgaria -0.578
4 Croatia  -0.712

Some data used:使用的一些数据:

#Data
df1 <- structure(list(cases = c(563.375758, 109.4, 0, 2), population = c(10.7969091, 
13.5885455, 5.5320606, 3.4548485), urbanisation = c(63.07388, 
99.38933, 85.84782, 64.21382), density = c(134.0869, 443.52112, 
52.30011, 60.46082), temperature = c(13.011172, 16.185297, 20.825068, 
20.855372), h_dev_index = c(0.9898, 0.9829455, 0.9669576, 0.9288667
)), class = "data.frame", row.names = c("Austria", "Belgium", 
"Bulgaria", "Croatia"))

df2 <- structure(list(V1 = c("(Intercept)", "population", "cases", "urbanisation", 
"density", "temperature", "h_dev_index"), V2 = c(-1.916, -0.007327, 
0.001473, 0.003798, 8.132e-05, -0.03518, 1.842), V3 = c(0.7144, 
0.001572, 0.0001544, 0.001962, 0.0002512, 0.008641, 0.88), V4 = c(-2.682, 
-4.659, 9.541, 1.936, 0.324, -4.071, 2.093), V5 = c("0.00785", 
"5.37e-06", "<", "0.05410", "0.74641", "6.43e-05", "0.03743"), 
    V6 = c("**", "***", "2e-16", ".", "", "***", "*"), V7 = c("", 
    "", "***", "", "", "", "")), class = "data.frame", row.names = c(NA, 
-7L))

Be careful about the names of your dataframes.请注意数据框的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM