R函数在第三种情况下将数据从一列复制到另一列

R function to copy data from one column to another under a third condition

我尝试根据值复制 long/lat 值(长 || 纬度),在 long_origin lat_origin 列(当前为空)中,但我做不到找到临时功能
任何帮助将不胜感激

数据

head(origin5,n = 15)
      Numéro Kanton     long      lat Kanton_origin long_origin lat_origin
1  PGV02.064     FR 571183.5 174640.0            VD                       
2  PGV02.064     NE 548363.0 206421.0            VD                       
3  PGV02.064     VD 539884.5 159601.0            VD                       
4  PGV02.064     VS 614169.5 122992.5            VD                       
5  PGV02.026     GE 499192.0 122622.5            VD                       
6  PGV01.079     VD 539884.5 159601.0            VD                       
7  PGV02.003     VD 539884.5 159601.0            VD                       
8  PGV01.108     VD 539884.5 159601.0            VD                       
9  PGV02.036     BE 616990.0 187209.0            FR                       
10 PGV03.026     FR 571183.5 174640.0            FR                       
11 PGV02.036     TI 702684.0 120482.0            FR                       
12 PGV02.036     VD 539884.5 159601.0            FR                       
13 PGV03.034     BE 616990.0 187209.0            NE                       
14 PGV03.034     GE 499192.0 122622.5            NE                       
15 PGV03.034     NE 548363.0 206421.0            NE 

预期结果:

      Numéro Kanton     long      lat Kanton_origin long_origin lat_origin
1  PGV02.064     FR 571183.5 174640.0            VD    539884.5  159601.0                    
2  PGV02.064     NE 548363.0 206421.0            VD    539884.5 159601.0                   
3  PGV02.064     VD 539884.5 159601.0            VD    539884.5 159601.0                  
4  PGV02.064     VS 614169.5 122992.5            VD    539884.5 159601.0                 
5  PGV02.026     GE 499192.0 122622.5            VD    539884.5 159601.0                   
6  PGV01.079     VD 539884.5 159601.0            VD    539884.5 159601.0                   
7  PGV02.003     VD 539884.5 159601.0            VD    539884.5 159601.0                   
8  PGV01.108     VD 539884.5 159601.0            VD    539884.5 159601.0                   
9  PGV02.036     BE 616990.0 187209.0            FR    571183.5 174640.0                   
10 PGV03.026     FR 571183.5 174640.0            FR    571183.5 174640.0                   
11 PGV02.036     TI 702684.0 120482.0            FR    571183.5 174640.0                   
12 PGV02.036     VD 539884.5 159601.0            FR    571183.5 174640.0                   
13 PGV03.034     BE 616990.0 187209.0            NE    548363.0 206421.0                    
14 PGV03.034     GE 499192.0 122622.5            NE    548363.0 206421.0                    
15 PGV03.034     NE 548363.0 206421.0            NE    548363.0 206421.0                    

您可以通过 dplyr::distinctdplyr::left_join 实现您想要的结果,如下所示:

library(dplyr)

origin5 %>% 
  left_join(distinct(origin5, Kanton, long, lat), by = c("Kanton_origin" = "Kanton"), suffix = c("", "_origin"))
#>       Numéro Kanton     long      lat Kanton_origin long_origin lat_origin
#> 1  PGV02.064     FR 571183.5 174640.0            VD    539884.5     159601
#> 2  PGV02.064     NE 548363.0 206421.0            VD    539884.5     159601
#> 3  PGV02.064     VD 539884.5 159601.0            VD    539884.5     159601
#> 4  PGV02.064     VS 614169.5 122992.5            VD    539884.5     159601
#> 5  PGV02.026     GE 499192.0 122622.5            VD    539884.5     159601
#> 6  PGV01.079     VD 539884.5 159601.0            VD    539884.5     159601
#> 7  PGV02.003     VD 539884.5 159601.0            VD    539884.5     159601
#> 8  PGV01.108     VD 539884.5 159601.0            VD    539884.5     159601
#> 9  PGV02.036     BE 616990.0 187209.0            FR    571183.5     174640
#> 10 PGV03.026     FR 571183.5 174640.0            FR    571183.5     174640
#> 11 PGV02.036     TI 702684.0 120482.0            FR    571183.5     174640
#> 12 PGV02.036     VD 539884.5 159601.0            FR    571183.5     174640
#> 13 PGV03.034     BE 616990.0 187209.0            NE    548363.0     206421
#> 14 PGV03.034     GE 499192.0 122622.5            NE    548363.0     206421
#> 15 PGV03.034     NE 548363.0 206421.0            NE    548363.0     206421

reprex package (v1.0.0)

于 2021-02-05 创建