如何使用expand_grid来复制值?

z5btuh9x  于 2023-02-10  发布在  其他
关注(0)|答案(3)|浏览(356)

我有以下两个数据框。
首先,我有职业数据框。下面是示例数据框

state <- c("00","00","32","32")
 codetype <- c("19","19","19","19")
 code <- c ("123456","123457","123456","123457")
 codetitle <- c("doctors","lawyers","doctors","lawyers")

 first <- data.frame(state,codetype,code,codetitle)

第二, Dataframe 是这个

state <- c("01","01","04","04","05","05")
 codetype <- c("19","19","19","19","19","19")
 code <- c("123456","123457","123456","123457","123456","123457")
 pct10 <- c(12.30,12.65,14.50,14.23,15.65,25.22)

 second <- data.frame(state,codetype,code,pct10)

所需的任务是这样的..需要在第一个数据框中创建新行。所需的结果是从第二个数据框中获取唯一的状态值,并在第一个数据框中创建相同的行..只是在开始时使用新的状态值。我知道我使用expand_grid。我唯一真实的困惑的是如何
预期结果

state       codetype       code       codetitle
      32            19          123456        Doctors 
      32            19          123457        Lawyers 
      00            19          123456        Doctors 
      00            19          123457        Lawyers 
      01            19          123456        Doctors 
      01            19          123457        Lawyers
      04            19          123456        Doctors 
      04            19          123457        Lawyers
      05            19          123456        Doctors 
      05            19          123457        Lawyers
1qczuiv0

1qczuiv01#

也许是这样:

library(dplyr)
second %>%
  select(-pct10) %>%
  distinct() %>%
  left_join(distinct(first, code, codetitle), by = "code") %>%
  bind_rows(first)
#    state codetype   code codetitle
# 1     01       19 123456   doctors
# 2     01       19 123457   lawyers
# 3     04       19 123456   doctors
# 4     04       19 123457   lawyers
# 5     05       19 123456   doctors
# 6     05       19 123457   lawyers
# 7     00       19 123456   doctors
# 8     00       19 123457   lawyers
# 9     32       19 123456   doctors
# 10    32       19 123457   lawyers
7hiiyaii

7hiiyaii2#

或者,您可以将plyr::rbind.fillleft join一起使用

third <-  plyr::rbind.fill(first,second) %>% select(-codetitle,-pct10) %>% 
left_join(first %>% select(code, codetitle) %>% unique(), by=c('code'))

创建于2023年2月6日,使用reprex v2.0.2

state codetype   code codetitle
1     00       19 123456   doctors
2     00       19 123457   lawyers
3     32       19 123456   doctors
4     32       19 123457   lawyers
5     01       19 123456   doctors
6     01       19 123457   lawyers
7     04       19 123456   doctors
8     04       19 123457   lawyers
9     05       19 123456   doctors
10    05       19 123457   lawyers
djmepvbi

djmepvbi3#

可以使用expand.grid()函数创建所需的结果:

state_codes <- unique(second$state)
expanded_grid <- expand.grid(state = state_codes, codetype = first$codetype[1], 
                             code = first$code, codetitle = first$codetitle)

result <- rbind(first, expanded_grid)
result

   state codetype   code codetitle
1     00       19 123456   doctors
2     00       19 123457   lawyers
3     32       19 123456   doctors
4     32       19 123457   lawyers
5     01       19 123456   doctors
6     04       19 123456   doctors
7     05       19 123456   doctors
8     01       19 123457   doctors
9     04       19 123457   doctors
10    05       19 123457   doctors
11    01       19 123456   doctors
12    04       19 123456   doctors
13    05       19 123456   doctors
14    01       19 123457   doctors
15    04       19 123457   doctors

相关问题