R: Combining identical rows into one (preferably using dplyr/tidyr)

Bønding

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?

ulfelder

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Combining multiple rows into one using precedence rules

From Dev

Combining multiple identical SQLite queries into one

From Dev

Combining multiple identical SQLite queries into one

From Dev

Combining Two Rows with Different Levels according to Some Conditions into One in R

From Dev

Combining rows of a list in R

From Dev

Combining Rows into List in R

From Dev

Combining Rows into List in R

From Dev

Talend - Combining two rows into one

From Dev

r - count number of identical rows

From Dev

Combining rows based on the id in R

From Dev

combining two consecutive rows using

From Dev

Combining data using R

From Dev

Using table data in SSMS'08 as an '03 access form header and combining several rows into one. Audit table

From Dev

Combining two rows from a table into one

From Dev

Combining multiple rows in postgreSQL into one row?

From Dev

Combining data from multiple rows into one

From Dev

SQL Combining multiple rows into one column

From Dev

combining multiple rows into one row in sql server

From Dev

Combining data from different rows into one variable

From Dev

Combining multiple rows into one single row

From Dev

Combining a variable amount of rows into one row?

From Dev

MySQL Combining selected columns of multiple rows into one

From Dev

Combining every pair of rows into one MYSQL

From Dev

R combining multiple Matrix in to one

From Dev

R combining two vectors into one

From Dev

Combining several columns into one (R)

From Dev

Combining two columns into one R

From Dev

Combining several columns into one (R)

From Dev

combining two columns to one in R