I have an issue I can't seem to figure out
I have a data frame like this
df <- data.frame(c(rep_len("a",3), "b", "b"), c(rep_len(55, 3), 44, 44),c(rep_len(12, 3), 6, 6), c("na", 2, "na", 3, "na"), c("na", "na", 4, "na", 8), c(5, "na", "na", "na", "na"))
names(df) <- c("street", "latitude", "longitude", "A", "B", "C")
street latitude longitude A B C
a 55 12 na na 5
a 55 12 2 na na
a 55 12 na 4 na
b 44 6 3 na na
b 44 6 na 8 na
and I guess what I'm looking for is a way to collapse rows with identical values in 'street', 'latitude', 'longitude' so the data frame looks like this
street latitude longitude A B C
a 55 12 2 4 5
b 44 6 3 8 na
My best attempt is this:
df %>%
group_by(street) %>%
summarise_each(funs(first))
But its not quite right. Have any ideas?
This works without reshaping and using just dplyr
, as long as you use the standard NA
in place of your "na"
and specify stringsAsFactors=FALSE
when creating df
:
df %>%
group_by(street, latitude, longitude) %>%
summarise_each(funs(ifelse(sum(is.na(.)==FALSE)==0, NA, .[which(is.na(.)==FALSE)])), matches("[A-Z]{1}"))
# Result
street latitude longitude A B C
1 a 55 12 2 4 5
2 b 44 6 3 8 NA
If you'd prefer to stick with "na"
, then this works:
df %>%
group_by(street, latitude, longitude) %>%
summarise_each(funs(ifelse(sum(.!="na")==0, "na", .[which(.!="na")])), matches("[A-Z]{1}"))
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments