简体   繁体   中英

How to combine two rows based on multiple columns in a dataframe?

The title says it all, I have a large dataset that consists of factory and latitude and longitude, and among others. some of the factories I find have identical lat long although their name slightly different. How can I combine rows of factories that have the same lat-long in R?

mill latitude longitude ID
a. 12.34. 7.86. NA
A. 12.34. 7.86. 4
b 47.56. 27.07. 5.

The output I am looking for is:

mill latitude longitude ID
A. 12.34. 7.86. 4
b. 47.56. 27.07. 5

Base R

aggregate(.~latitude+longitude,df,tail,1)

  latitude longitude mill ID
1    12.34      7.86   A.  4
2    47.56     27.07    b  5

We can use

library(dplyr)
df1 %>%
  arrange(latitude, longitude, is.na(ID)) %>%
  distinct(latitude, longitude, .keep_all = TRUE)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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