如何从表/矩阵向数据集添加值?

问题描述 投票:0回答:2

我需要同时根据两列的值向我的数据集添加值

这是我的数据集样本

# A tibble: 15 × 5
# Groups:   nquest, nord [15]
   nquest  nord tpens  ireg sector
    <int> <int> <dbl> <int>  <dbl>
 1    173     1  1800    18      1
 2   2886     1  1211    13      4
 3   2886     2  2100    13      3
 4   5416     1   700     8      4
 5   7886     1  2000     9      2
 6  20297     1  1200     5      2
 7  20711     2  2000     4      3
 8  22169     1   880    15      4
 9  22276     1  1200     8      1
10  22286     1   850     8      4
11  22286     2   650     8      2
12  22657     1  1400    16      4
13  22657     2  1500    16      1
14  23490     1  1400     5      1
15  24147     1  1730     4      4

我需要根据他具体拥有的变量

ireg
sector
的值将下表的值添加到数据集的每个个体

             sector == 1   sector == 2  sector == 3   sector ==4
ireg == 1        1944.5       27973.4        5328.4      79542.3
ireg == 2          49.3         561.7         237.1       3132.8
ireg == 3        3785.3       73596.3       13563.1     240420.8
ireg == 4        1661.1        7157.5        2054.2      27174.2
ireg == 5        3016.1       37700.8        6429.1      89338.0
ireg == 6         504.5        8160.5        1386.9      22491.7
ireg == 7         442.6        6954.7        2160.0      32398.8
ireg == 8        3419.0       36172.4        5416.6      89411.6
ireg == 9        2266.7       20603.2        4269.7      74176.9
ireg == 10        551.7        4019.0        1060.2      13843.0
ireg == 11        642.8        9484.3        1569.9      23637.0
ireg == 12       1962.6       17565.5        5863.1     145318.8
ireg == 13        843.9        5562.1        1520.3      19911.1
ireg == 14        308.7         831.5         295.6       4021.4
ireg == 15       2524.2       12507.0        4441.0      75844.6
ireg == 16       2763.3        8485.3        3581.4      49263.3
ireg == 17        607.4        2481.2         555.3       6689.7
ireg == 18       1474.0        2272.0        1217.7      23005.5
ireg == 19       3303.8        6552.2        3444.8      63552.4
ireg == 20       1232.1        3212.3        1449.7      23478.9

我已经尝试为每个单元格使用

case_when
手动添加它们,但是它们是 80 并且太长了......

dataset <- dataset %>%
  mutate(VA = case_when(
    (ireg == 1 & nace == 1) ~ 1944.5,
    (ireg == 1 & nace == 2 ) ~ 27973.4,
    (ireg == 1 & nace == 3 ) ~ 5328.4,
    (ireg == 1 & nace == 4 ) ~ 79542.3,
    (ireg == ... & nace == ... ) ~ ....

有更快的方法吗??

r variables dplyr merge dataset
2个回答
1
投票

您可以通过

mutate()
电话访问您的矩阵:

library(dplyr)
library(readr)

dataset %>% 
  rowwise() %>% 
  mutate(VA = m[ireg, sector]) %>% 
  ungroup()
#> # A tibble: 15 × 6
#>    nquest  nord tpens  ireg sector     VA
#>     <dbl> <dbl> <dbl> <dbl>  <dbl>  <dbl>
#>  1    173     1  1800    18      1  1474 
#>  2   2886     1  1211    13      4 19911.
#>  3   2886     2  2100    13      3  1520.
#>  4   5416     1   700     8      4 89412.
#>  5   7886     1  2000     9      2 20603.
#>  6  20297     1  1200     5      2 37701.
#>  7  20711     2  2000     4      3  2054.
#>  8  22169     1   880    15      4 75845.
#>  9  22276     1  1200     8      1  3419 
#> 10  22286     1   850     8      4 89412.
#> 11  22286     2   650     8      2 36172.
#> 12  22657     1  1400    16      4 49263.
#> 13  22657     2  1500    16      1  2763.
#> 14  23490     1  1400     5      1  3016.
#> 15  24147     1  1730     4      4 27174.

示例数据:

dataset <- read_table("
nquest  nord tpens  ireg sector
   173     1  1800    18      1
  2886     1  1211    13      4
  2886     2  2100    13      3
  5416     1   700     8      4
  7886     1  2000     9      2
 20297     1  1200     5      2
 20711     2  2000     4      3
 22169     1   880    15      4
 22276     1  1200     8      1
 22286     1   850     8      4
 22286     2   650     8      2
 22657     1  1400    16      4
 22657     2  1500    16      1
 23490     1  1400     5      1
 24147     1  1730     4      4")

m <- read_table(col_names = F, "
1944.5       27973.4        5328.4      79542.3
  49.3         561.7         237.1       3132.8
3785.3       73596.3       13563.1     240420.8
1661.1        7157.5        2054.2      27174.2
3016.1       37700.8        6429.1      89338.0
 504.5        8160.5        1386.9      22491.7
 442.6        6954.7        2160.0      32398.8
3419.0       36172.4        5416.6      89411.6
2266.7       20603.2        4269.7      74176.9
 551.7        4019.0        1060.2      13843.0
 642.8        9484.3        1569.9      23637.0
1962.6       17565.5        5863.1     145318.8
 843.9        5562.1        1520.3      19911.1
 308.7         831.5         295.6       4021.4
2524.2       12507.0        4441.0      75844.6
2763.3        8485.3        3581.4      49263.3
 607.4        2481.2         555.3       6689.7
1474.0        2272.0        1217.7      23005.5
3303.8        6552.2        3444.8      63552.4
1232.1        3212.3        1449.7      23478.9") %>% as.matrix()

创建于 2023-03-12 与 reprex v2.0.2


1
投票

base R
,我们可以做到

dat$VA <- m1[as.matrix(dat[c("ireg", "sector")])]

-输出

> dat
# A tibble: 15 × 6
   nquest  nord tpens  ireg sector     VA
    <dbl> <dbl> <dbl> <dbl>  <dbl>  <dbl>
 1    173     1  1800    18      1  1474 
 2   2886     1  1211    13      4 19911.
 3   2886     2  2100    13      3  1520.
 4   5416     1   700     8      4 89412.
 5   7886     1  2000     9      2 20603.
 6  20297     1  1200     5      2 37701.
 7  20711     2  2000     4      3  2054.
 8  22169     1   880    15      4 75845.
 9  22276     1  1200     8      1  3419 
10  22286     1   850     8      4 89412.
11  22286     2   650     8      2 36172.
12  22657     1  1400    16      4 49263.
13  22657     2  1500    16      1  2763.
14  23490     1  1400     5      1  3016.
15  24147     1  1730     4      4 27174.
© www.soinside.com 2019 - 2024. All rights reserved.