[英]R function to copy data from one column to another under a third condition
I try to copy the long/lat values (long || lat), in the column long_origin lat_origin (currently empty), based on the value and I can't find the ad hoc function我尝试根据值复制 long_origin lat_origin 列(当前为空)中的 long/lat 值(long || lat),但我找不到临时 function
any help would be appreciated任何帮助,将不胜感激
Data数据
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
expected result:预期结果:
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
You could achieve your desired result via dplyr::distinct
and dplyr::left_join
like so:您可以通过
dplyr::distinct
和dplyr::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
Created on 2021-02-05 by the reprex package (v1.0.0)由代表 package (v1.0.0) 于 2021 年 2 月 5 日创建
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.