0

I have a list of data. I want to use aggregate and dcast in Rstudio for the list. I have used this one for data frames.

dcast(df, PROVINCE ~ ETHNICITY, value.var = "WEIGHT", fun.aggregate = sum)
  PROVINCE      1     2      3     4      5    6        8 11    12    67   99   113   114 1016 1106
1       11 845.19 41.43 214.38 93.66 273.78 19.7 13656.12 48 10.43  0.00 0.00 10.12 20.40   30 19.9
2       14   0.00  0.00   0.00  0.00   0.00  0.0     0.00  0  0.00  0.00 0.00  0.00  9.89    0  0.0
3       16   0.00  0.00   0.00  0.00   0.00  0.0     0.00  0  0.00 10.11 0.00  0.00  0.00    0  0.0
4       19   0.00  0.00   0.00  0.00   0.00  0.0     0.00  0  0.00  0.00 9.57  0.00  0.00    0  0.0
5       32  29.58  0.00   0.00  0.00   0.00  0.0     0.00  0  0.00  0.00 0.00  0.00  9.78    0  0.0
6       33   0.00  0.00   0.00  0.00   0.00  0.0     0.00  0  0.00  0.00 0.00  0.00 49.39    0  0.0
7       35   0.00  0.00   0.00  0.00   0.00  0.0     0.00  0  0.00  0.00 0.00  0.00 49.63    0  0.0
8       63   0.00  0.00   0.00  0.00   0.00  0.0     0.00  0  0.00  0.00 0.00  0.00  9.43    0  0.0


## I want to use that code for a list of data in R, but I do not know how to use it ##
Kian
  • 110
  • 7
  • It's not clear what you're trying to output. Do you want a foruth data frame containing all the common rows in `df1`, `df2` and `df3`? – anddt Sep 29 '20 at 09:24
  • In good old base R `merge()` is the function you are looking for. See the documentation here (https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/merge) and here (https://www.dummies.com/programming/r/how-to-use-the-merge-function-with-data-sets-in-r/). If you prefer to work with tidyverse, then you may want to check out the `join_` family of functions in the `dplyr` package – davidnortes Sep 29 '20 at 09:31
  • @anddt I have added the output that I want. ** thank you for your comment** – Kian Sep 29 '20 at 09:36
  • @davidnortes But there are many many duplicates and tripes rows. ~~join~~ will displace the rows. Thanks for your attention. – Kian Sep 29 '20 at 09:38
  • @Samira Also, it seems your three dataframes have different columns (can't really tell). I would share a snippet of the data and give an example of the desired output. [Here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for more details on how to jot down a reproducible exaple. – anddt Sep 29 '20 at 09:43
  • I think that, the main question here is: how would you identify the correspondence between observations across datasets, without one or more common IDs? Once you know that, you just have to merge with the appropriate parameters. – Cainã Max Couto-Silva Nov 30 '20 at 07:37
  • @CainãMaxCouto-Silva. That is the question. I need the ``` DAILY_LANG``` column in df1, I tried to create an ID for them by using ```paste``` function but I got many duplicates and triples. – Kian Nov 30 '20 at 08:45

1 Answers1

0

If you're trying to extract all the common rows in all the data frames you might consider adding them to a list and reduce it via inner_join (from {dplyr}). Here a very simple example:

library(tibble)
library(purrr)
library(dplyr)

# Declare three data frames
df1 <- tribble(
    ~id, ~height, ~size,
    "123", 43, 12,
    "456", 54, 13,
    "789", 54, 34
)

df2 <- tribble(
    ~id, ~height, ~size,
    "123", 43, 12,
    "424", 43, 14,
    "949", 12, 43
)

df3 <- tribble(
    ~id, ~height, ~size,
    "123", 43, 12,
    "932", 94, 7
)

# Add them to a list
df_list <- list(df1, df2, df3)

# Find common rows across the three dfs
reduce(df_list, inner_join)
#> Joining, by = c("id", "height", "size")
#> Joining, by = c("id", "height", "size")
#> # A tibble: 1 x 3
#>   id    height  size
#>   <chr>  <dbl> <dbl>
#> 1 123       43    12
anddt
  • 1,589
  • 1
  • 9
  • 26
  • I just want to add ``` DAILY_LANG``` column from df2 to df1. As you see df2 has column ```DAILY_LANG``` but df1 does not. I want to add ```DAILY_LANG``` to df1. – Kian Nov 30 '20 at 08:41