R语言 如何使用一年的特定变量并将其与其他年份相匹配?

f3temu5u  于 2023-05-04  发布在  其他
关注(0)|答案(1)|浏览(191)

在面板数据中,我有2009-10年度的地区代码。然而,基于公共id变量,我希望其他轮次(2014年至2019年)也使用相同的地区代码。我想不出在R中有什么方法。任何线索或建议将不胜感激。

data <- tibble::tibble(
  Year = rep(c("2010", "2014", "2015"), each = 5L),
  id = c(
    10001, 10002, 10003, 10004, 10005, 100001, 100002, 100003, 100004, 100005,
    100001, 100002, 100003, 100004, 100005
  ),
  dist = rep(c(13, NA), c(5L, 10L))
)
b4lqfgs4

b4lqfgs41#

如果您打算将NA s替换为其他年份共享id s的 dist
使用稍微修改过的数据集

df
# A tibble: 15 × 3
   Year      id  dist
   <chr>  <dbl> <dbl>
 1 2010  100001    13
 2 2010  100002    13
 3 2010  100003    13
 4 2010  100004    13
 5 2010  100005    13
 6 2014  100001    NA
 7 2014  100002    NA
 8 2014  100003    NA
 9 2014  100004    NA
10 2014  100005    NA
11 2015  100001    NA
12 2015  100002    NA
13 2015  100003    NA
14 2015  100004    NA
15 2015  100005    NA

使用fill

library(dplyr)
library(tidyr)

df %>% 
  group_by(id) %>% 
  fill(dist) %>% 
  ungroup()
# A tibble: 15 × 3
   Year      id  dist
   <chr>  <dbl> <dbl>
 1 2010  100001    13
 2 2010  100002    13
 3 2010  100003    13
 4 2010  100004    13
 5 2010  100005    13
 6 2014  100001    13
 7 2014  100002    13
 8 2014  100003    13
 9 2014  100004    13
10 2014  100005    13
11 2015  100001    13
12 2015  100002    13
13 2015  100003    13
14 2015  100004    13
15 2015  100005    13
数据
df <- structure(list(Year = structure(c("2010", "2010", "2010", "2010", 
"2010", "2014", "2014", "2014", "2014", "2014", "2015", "2015", 
"2015", "2015", "2015"), label = "Year", format.stata = "%-12s"), 
    id = structure(c(100001, 100002, 100003, 100004, 100005, 
    100001, 100002, 100003, 100004, 100005, 100001, 100002, 100003, 
    100004, 100005), label = "DSL", format.stata = "%10.0g"), 
    dist = structure(c(13, 13, 13, 13, 13, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA), label = "District Code", format.stata = "%2.0f")), 
    row.names = c(NA, -15L), class = c("tbl_df", "tbl", "data.frame"))

相关问题