Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R-将值从特定行中的特定列传输到倾向分数匹配中的匹配ID_R_Join_Dplyr_Match - Fatal编程技术网

R-将值从特定行中的特定列传输到倾向分数匹配中的匹配ID

R-将值从特定行中的特定列传输到倾向分数匹配中的匹配ID,r,join,dplyr,match,R,Join,Dplyr,Match,我有两个数据框:一个包含使用倾向评分匹配匹配的成对参与者的ID(即,每行有两个匹配的ID;df1),另一个包含所有参与者的长格式的纵向数据(df2) 在每对配对中,一人来自实验组,另一人来自对照组。组由变量Group表示。在df2中,来自实验组的参与者在变量年和月上有值,而来自控制组的参与者在这些变量上只有NA。我现在的目标是将实验组参与者的年和月上的值复制到对照组的匹配伙伴(基于df1中匹配ID的信息) 非常感谢您的帮助 这是实现这一任务的一种可能的解决方案。基本上,我们首先创建一个looku

我有两个数据框:一个包含使用倾向评分匹配匹配的成对参与者的ID(即,每行有两个匹配的ID;
df1
),另一个包含所有参与者的长格式的纵向数据(
df2

在每对配对中,一人来自实验组,另一人来自对照组。组由变量
Group
表示。在df2中,来自实验组的参与者在变量
上有值,而来自控制组的参与者在这些变量上只有NA。我现在的目标是将实验组参与者的
上的值复制到对照组的匹配伙伴(基于
df1
中匹配ID的信息)


非常感谢您的帮助

这是实现这一任务的一种可能的解决方案。基本上,我们首先创建一个
lookup
表,其中带有
Year
Month
的每个ID与其不带
Year
Month
的相对参与者配对。然后,我们使用一个左连接并将
Year
Month
NA
值合并,以获得一个新列,而不丢失值

library(dplyr)

lookup <- df1 %>% inner_join(df2, by = c("ID_EG" = "ID")) %>% select(-Group) %>% distinct()
#    ID_EG  ID_CG Year Month
# 1 800057 834341 2008     2
# 2 800119 897177 2014    10
# 3 800125 834011 2010     5

df2 %>%
  left_join(lookup, by = c("ID" = "ID_CG")) %>% 
  mutate(
    Year = coalesce(Year.x, Year.y),
    Month = coalesce(Month.x, Month.y)
    ) %>% 
  select(!ends_with(".x") & !ends_with(".y"), -ID_EG)

coalesce
对于这个问题来说是一个非常好的函数!
df2 <- read.table(text=
"ID       Group    Year      Month
800057    1        2008      2         
800057    1        2008      2   
800057    1        2008      2   
800057    1        2008      2   
800057    1        2008      2   
800119    1        2014      10  
800119    1        2014      10         
800119    1        2014      10   
834011    0        NA        NA  
834011    0        NA        NA    
834341    0        NA        NA   
834341    0        NA        NA   
834341    0        NA        NA    
834341    0        NA        NA    
834341    0        NA        NA   
800125    1        2010     5
800125    1        2010     5
897177    0        NA       NA
897177    0        NA       NA
897177    0        NA       NA", header=TRUE)
df3 <- read.table(text=
"ID       Group    Year      Month
800057    1        2008      2         
800057    1        2008      2   
800057    1        2008      2   
800057    1        2008      2   
800057    1        2008      2   
800119    1        2014      10  
800119    1        2014      10         
800119    1        2014      10   
834011    0        2010      5  
834011    0        2010      5    
834341    0        2008      2   
834341    0        2008      2   
834341    0        2008      2    
834341    0        2008      2    
834341    0        2008      2   
800125    1        2010      5
800125    1        2010      5
897177    0        2014      10
897177    0        2014      10
897177    0        2014      10", header=TRUE) 
library(dplyr)

lookup <- df1 %>% inner_join(df2, by = c("ID_EG" = "ID")) %>% select(-Group) %>% distinct()
#    ID_EG  ID_CG Year Month
# 1 800057 834341 2008     2
# 2 800119 897177 2014    10
# 3 800125 834011 2010     5

df2 %>%
  left_join(lookup, by = c("ID" = "ID_CG")) %>% 
  mutate(
    Year = coalesce(Year.x, Year.y),
    Month = coalesce(Month.x, Month.y)
    ) %>% 
  select(!ends_with(".x") & !ends_with(".y"), -ID_EG)
       ID Group Year Month
1  800057     1 2008     2
2  800057     1 2008     2
3  800057     1 2008     2
4  800057     1 2008     2
5  800057     1 2008     2
6  800119     1 2014    10
7  800119     1 2014    10
8  800119     1 2014    10
9  834011     0 2010     5
10 834011     0 2010     5
11 834341     0 2008     2
12 834341     0 2008     2
13 834341     0 2008     2
14 834341     0 2008     2
15 834341     0 2008     2
16 800125     1 2010     5
17 800125     1 2010     5
18 897177     0 2014    10
19 897177     0 2014    10
20 897177     0 2014    10
df <- df1 %>% 
  left_join(df2 %>% select(-Group), by = c("ID_EG" = "ID")) %>% 
  unique() %>% 
  pivot_longer(contains("ID"), values_to = "ID", names_to = "Group") %>% 
  mutate(Group = ifelse(Group == "ID_EG", 1, 0)) %>% 
  left_join(df2, ., by = "ID") %>%
  select(-contains(".x")) %>%
  data.table::setnames(str_subset(names(.), ".y"), str_subset(names(.), ".y") %>% str_remove(".y"))
       ID Year Month Group
1  800057 2008     2     1
2  800057 2008     2     1
3  800057 2008     2     1
4  800057 2008     2     1
5  800057 2008     2     1
6  800119 2014    10     1
7  800119 2014    10     1
8  800119 2014    10     1
9  834011 2010     5     0
10 834011 2010     5     0
11 834341 2008     2     0
12 834341 2008     2     0
13 834341 2008     2     0
14 834341 2008     2     0
15 834341 2008     2     0
16 800125 2010     5     1
17 800125 2010     5     1
18 897177 2014    10     0
19 897177 2014    10     0
20 897177 2014    10     0