'merge rows with same values
I'm new to R and I would like to merge those two data frame, because the rows have the same X and Y values :
#df1
OBJECTID urban_sprawl_copy X Y agriculture_sprawl forest_sprawl
158448 0 1235546 1100801 0 0
158510 0 1235546 1100513 0 0
158447 0 1235258 1100801 0 0
158509 0 1235258 1100513 0 0
158571 0 1235258 1100225 0 0
62247 0 1234970 1088991 0 0
#df2
X Y dist_to_urban_2015
1235546 1100801 40963.63
1235546 1100513 40753.46
1235258 1100801 40767.71
1235258 1100513 40556.52
1235258 1100225 40346.29
1234970 1100801 40572.89
I do so with this :
merged <- merge(df1, df2, all=TRUE)
But I end up with something like this :
#merged
X Y OBJECTID urban_sprawl_copy agriculture_sprawl forest_sprawl dist_to_urban_2015
1235546 1100513 158510 0 0 0 NA
1235546 1100801 158448 0 0 0
1235546 1100513 NA NA NA NA 40753.46
1235546 1100801 NA NA NA NA 40753.46
1235258 1100225 158571 0 0 0 NA
1235258 1100513 158509 0 0 0 NA
I would like to merge the rows with same X and Y values, like seen in this thread.
So using something like this
Myfunc <- function(x) x[x != '']
df %>%
group_by(X,Y) %>%
summarise_each(funs(Myfunc))
But it does'nt seem to work. Does anyone have an idea why ?
This is a simplified version because I have like 25 variables and I'm new to stackoverflow, so excuse me in advance if it doesn't fit with the rules of publication.
Thanks!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
