2

I have these two datasets that I am trying to append:

data1 = structure(list(year = c(2017, 2018), flow = c("Export", "Export"
), EUR = c(4, 3.44), Home = c(3.09, 3.03), Not_reported = c(0.12, 
0), USD = c(92.29, 93.04), country = c("Brazil", "Brazil"), Other = c(0.499999999999994, 
0.489999999999994)), row.names = c(NA, -2L), vars = c("year", 
"flow"), drop = TRUE, indices = list(0L, 1L), group_sizes = c(1L, 
1L), biggest_group_size = 1L, labels = structure(list(year = c(2017, 
2018), flow = c("Export", "Export")), row.names = c(NA, -2L), vars = c("year", 
"flow"), drop = TRUE, indices = list(0L, 1L), group_sizes = c(1L, 
1L), biggest_group_size = 1L, labels = structure(list(year = c(2017, 
2018), flow = c("EXP", "EXP")), class = "data.frame", row.names = c(NA, 
-2L), vars = c("year", "flow"), drop = TRUE), class = "data.frame"), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"))
data2 = structure(list(flow = c("Export", "Export", "Export", "Export", 
"Export", "Import"), country = structure(c(6L, 6L, 6L, 6L, 6L, 
6L), .Label = c("Algeria", "Argentina", "Australia", "Austria", 
"Belgium", "Brazil", "Bulgaria", "Canada", "China", "Colombia", 
"Cyprus", "Czech Republic", "Denmark", "Estonia", "Euro", "Finland", 
"France", "Germany", "Greece", "Hungary", "Iceland", "India", 
"Indonesia", "Ireland", "Israel", "Italy", "Japan", "Latvia", 
"Lithuania", "Luxembourg", "Malaysia", "Malta", "Morocco", "Netherlands", 
"Pakistan", "Poland", "Portugal", "Romania", "Slovakia", "Slovenia", 
"South Africa", "South Korea", "Spain", "Sweden", "Switzerland", 
"Thailand", "Ukraine", "United Kingdom", "United States"), class = "factor"), 
    year = c(2007, 2008, 2009, 2010, 2011, 2007), EUR = c(4.76, 
    4.95, 4.51, 4.28, 3.8, 11.1), Home = c(0.13, 0.16, 1.11, 
    0.82, NA, 0.48), Not_reported = c(NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_), USD = c(94.7, 94.4, 93.8, 
    94.3, 94.5, 85.5), Other = c(NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_)), row.names = c(NA, 6L), class = "data.frame")

When I tried:

rbind(data1, data2)

I got a list instead of a dataframe. I have checked the class of each column and they seem consistent with each other. Can someone explain to me? Thanks!

Min Nguyen
  • 45
  • 3
  • 2
    It's because `data1` is grouped. You can `rbind(ungroup(data1), data2)` or `rbind(as.data.frame(data1), data2)`. – neilfws Oct 10 '19 at 21:29
  • How do we determine if a dataset is grouped or not? – Min Nguyen Oct 10 '19 at 21:33
  • 2
    `class(data1)` will tell you if it is grouped. `class` and `str` are very useful functions to use in general. if you want to continue with tidyverse and learn less uselfil functions, `is.grouped_df(data1)` will be helpful in this particular case, but I can think of no other cases where it would be – rawr Oct 10 '19 at 21:40
  • 1
    @computercarguy there is no point in formatting this type of question, this is a very common way of sharing R objects, and asking users, especially new ones, to make it more human-readable is wasted effort – rawr Oct 10 '19 at 21:43
  • @computercarguy: thanks for the edit but I honestly thought that for this type of question you'd have to reconstruct the data to see the underlying problem anyway, so I didn't bother to format it. – Min Nguyen Oct 10 '19 at 21:51
  • @min-nguyen `class(data1)` should return `grouped_df` as one of the classes. There's also a dplyr function `is_grouped_df()`. And of course, you must have run `group_by` in your code at some point :) – neilfws Oct 10 '19 at 21:54
  • There is nothing wrong with the formatting. The output from `dput()` is purely to copy/paste, it doesn't have to look nice. – neilfws Oct 10 '19 at 21:55
  • @computercarguy I don't disagree with you, but this particular code is automatically generated via a function, see ["copy your data"](https://stackoverflow.com/a/5963610/2994949) section and generally not the source of errors per se – rawr Oct 10 '19 at 21:55
  • @computercarguy worth pointing out however that in the context of this question, pasting two text tables (rather than the `dput` output) doesn't make this problem reproducible because the grouping won't be represented. – Mako212 Oct 10 '19 at 22:05

0 Answers0