Combine data df1, df2, df3 in R in the specified column

In R, I have df1, df2 and df3, which are thunderstorms. Each df has two columns: "city" and "wounds."

df1 = data.frame(city=c("atlanta", "new york"), injuries=c(5,8))
df2 = data.frame(city=c("chicago", "new york"), injuries=c(2,3))
df3 = data.frame(city=c("los angeles", "atlanta"), injuries=c(1,7))

I want to combine all 3 data frames by type of external connection in the city column so that all cities are displayed in a combined data frame, and the injury count will be summarized as follows:

combined.df

city         df1.freq   df2.freq   df3.freq
atlanta      5          0          7
new york     8          3          0
chicago      0          2          0
los angeles  0          0          1
+4
source share
4 answers

Alternative to @flodel version using the basic R function reshape:

dat <- list(df1,df2,df3)
intm <- data.frame(do.call(rbind,dat),val=rep(seq_along(dat),sapply(dat,nrow)))
reshape(intm, idvar="city", timevar="val", direction="wide")

#         city injuries.1 injuries.2 injuries.3
#1     atlanta          5         NA          7
#2    new york          8          3         NA
#3     chicago         NA          2         NA
#5 los angeles         NA         NA          1
+1
source

This is common to any number of data.frames:

library(functional)
Reduce(Curry(merge, by = "city", all = TRUE), list(df1, df2, df3))
#          city injuries.x injuries.y injuries
# 1     atlanta          5         NA        7
# 2    new york          8          3       NA
# 3     chicago         NA          2       NA
# 4 los angeles         NA         NA        1

. :

df.long <- do.call(rbind, Map(transform, list(df1, df2, df3),
                                         name = c("df1", "df2", "df3")))
#          city injuries name
# 1     atlanta        5  df1
# 2    new york        8  df1
# 3     chicago        2  df2
# 4    new york        3  df2
# 5 los angeles        1  df3
# 6     atlanta        7  df3

xtabs, :

xtabs(injuries ~ city + name, df.long)
#              name
# city          df1 df2 df3
#   atlanta       5   0   7
#   new york      8   3   0
#   chicago       0   2   0
#   los angeles   0   0   1

( reshape , .)

+4

merge - . ?merge .

> merge(merge(df1, df2, by = "city", all = TRUE), df3, by = "city", all = TRUE)
         city injuries.x injuries.y injuries
1     atlanta          5         NA        7
2     chicago         NA          2       NA
3 los angeles         NA         NA        1
4    new york          8          3       NA

Edit. @flodel, , :

 Reduce(function(d1, d2) merge(d1, d2, all = TRUE, by = "city"), list(df1, df2, df3))
+2

reshape::cast (, @thelatemail!). id , , :

df1$id <- 'df1.freq'
df2$id <- 'df2.freq'
df3$id <- 'df3.freq'

rb <- rbind(df1,df2,df3)
library(reshape)
cast(rb, city ~ id, value='injuries')

:

         city df1.freq df2.freq df3.freq
1     atlanta        5       NA        7
2    new york        8        3       NA
3     chicago       NA        2       NA
4 los angeles       NA       NA        1
+1

All Articles