I have two data.frames in r.THe first one is
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|)
(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):
-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:
# 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.