[英]How to combine two rows based on multiple columns in a dataframe?
標題說明了一切,我有一個大型數據集,其中包括工廠和緯度和經度等。 我發現一些工廠的經緯度相同,盡管它們的名稱略有不同。 如何在 R 中組合具有相同經緯度的工廠行?
磨 | 緯度 | 經度 | ID |
---|---|---|---|
一個。 | 12.34。 | 7.86。 | 不適用 |
一個。 | 12.34。 | 7.86。 | 4 |
b | 47.56。 | 27.07。 | 5. |
我正在尋找的 output 是:
磨 | 緯度 | 經度 | ID |
---|---|---|---|
一個。 | 12.34。 | 7.86。 | 4 |
灣。 | 47.56。 | 27.07。 | 5 |
底座 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
我們可以用
library(dplyr)
df1 %>%
arrange(latitude, longitude, is.na(ID)) %>%
distinct(latitude, longitude, .keep_all = TRUE)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.