Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
263 views
in Technique[技术] by (71.8m points)

tidyr - 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
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                    
question from:https://stackoverflow.com/questions/66067611/r-function-to-copy-data-from-one-column-to-another-under-a-third-condition

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You could achieve your desired result via dplyr::distinct and dplyr::left_join like so:

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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...