简体   繁体   English

列重新排列 csv 文件 R 或 Excel

[英]Column Rearrange csv file R or Excel

I have 2 data sets with the exact same coordinates that I am trying to merge into 1 by rearranging the coordinates to match while shifting the data with it.我有 2 个具有完全相同坐标的数据集,我试图通过重新排列坐标以在移动数据的同时将其合并为 1。 One set has coordinates associated with bioclim variables and another has the same coordinates associated with land ownership, but these coordinates are out of order (thank you GIS for the mixaro).一组具有与生物气候变量相关的坐标,另一组具有与土地所有权相关的相同坐标,但这些坐标是乱序的(感谢 GIS 的 mixaro)。 Any tips on how to rearrange it so the coordinates line up for easy dataset manipulation?关于如何重新排列它以便坐标对齐以便于数据集操作的任何提示? I was trying to do this in excel but if anyone knows an R trick that would be super!我试图在 excel 中做到这一点,但如果有人知道一个 R 技巧,那就太好了! ((Data pic example in the link)) ((链接中的数据图片示例))

数据集示例

You should use R data.table and merge the two tables by latitude and longitude.您应该使用 R data.table并按纬度和经度合并两个表。

library(data.table)
dt1=data.table(df1)
dt2=data.table(df2)
# converting the lat-long columns to characters
dt1[,`:=`(LATITUDE=as.character(LATITUDE), LONGITUDE=as.character(LONGITUDE))]
dt1[dt2,on=c("LATITUDE","LONGITUDE")]
  • performs a left join执行左连接

OR或者

dt=merge(dt1,dt2,by=c("LATITUDE","LONGITUDE"),all=T)
  • by takes the column to merge upon by获取要合并的列
  • all=T performs an outer join all=T执行外连接
  • Check the documentation of datatable merge for use of other parameters.https://www.rdocumentation.org/packages/data.table/versions/1.13.0/topics/merge检查数据表合并的文档以使用其他参数。 https://www.rdocumentation.org/packages/data.table/versions/1.13.0/topics/merge

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM