简体   繁体   English

R function 在第三种条件下将数据从一列复制到另一列

[英]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::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

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.

相关问题 如何根据R中的条件将数据从列复制到另一列? - How to copy data from a column to another based on a condition in R? 如何根据第三列的共享 ID 从一个数据框中获取数据并将其复制到另一个数据框中的现有列中 - How to take data from one dataframe and copy it into existing columns in another dataframe based on the shared ID of a third column 从 R dataframe 中的特定行将数据从一列复制到另一列 - Copy data from one column to another column from a specific row in R dataframe 函数基于R中另一列的数据检索一列的数据 - function to retrieve data from one column based on data from another column in R 如何在匹配R中的其他列时将特定值从一个数据列复制到另一个数据列? - How to copy specific values from one data column to another while matching other columns in R? 将唯一值从一列复制到另一列 - Copy unique values from one column to another R 按条件从另一列划分一列数据帧 - Divide one column of data frame by condition from another column R:如果另一列具有不同的值,如何使用aggregate()函数对某一列的数据求和? - R: how to use the aggregate()-function to sum data from one column if another column has a distinct value? 新列,其值来自R中的另一个名称,该名称位于R - new column with value from another one which name is in a third one in R 根据条件从R中的另一个数据表中提取列值 - Extracting column values based on condition from another data table in R
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM