0

I have 3 data sets as such

**DF1**                            
        pt1     pt2            
A       1.5     2.2   

**DF2**
        pt1      pt2      pt3  
B       40       22       23  


**DF3**
        pt2      pt3      pt4      pt5
C       23       43       12       65 
D       12       04       13       34


IDEAL OUTPUT

        pt1      pt2      pt3      pt4     pt5
A       1.5      2.2      NA       NA      NA
B       0        22       23       NA      NA
C       NA       23       43       12      65
D       NA       12       04       13      34

When I try to bind_rows however I get this error

df_list <- list(DF1, DF2, DF3)

df <- dplyr::bind_rows(df_list)

Error: Argument 1 must have names

I have tried looking this up but keep running into dead ends. Why am I getting this error and how can I fix it?

JVDeasyas123
  • 263
  • 3
  • 14
  • Could you print the output of `dput` applied to each of the 3 data frames? I believe something like `purrr::reduce(df_list, dplyr::bind_rows)` should work – iago Aug 11 '21 at 16:34
  • 1
    I cannot reproduce your problem, both `bind_rows(df1,df2,df3)` (not what you tried) and `bind_rows(list(df1,df2,df3))` work for me without error. To know why this is happening to you, it would be beneficial to have the "real" unambiguous structure of the frame. Please use `dput(.)` on each `df*` and post the output in your question. Thanks! – r2evans Aug 11 '21 at 16:34
  • 1
    Check this post: https://stackoverflow.com/questions/52505923/error-in-bind-rows-x-id-argument-1-must-have-names It might be that one or several of your dfs are built from unamed list, probably the first column with the capital letters. – Nicolás Velasquez Aug 11 '21 at 16:37
  • I do think the naming issue is the problem. I have thousands of columns, so is there a way I can post an abbreviated dput? – JVDeasyas123 Aug 11 '21 at 16:46
  • `dput(dat[1:3,1:5])`? – r2evans Aug 11 '21 at 16:47
  • DF1 dput```structure(list(pt1 = c(23, NA, NA), pt2 = c4, NA, NA), pt3 = c(23, NA, NA), pt4 = c(2, NA, NA), pt5 = c(1, NA, NA)), row.names = c("row one", "NA", "NA.1"), class = "data.frame")``` Im not sure what the NAs are, it is only one row... – JVDeasyas123 Aug 11 '21 at 16:51
  • It is the same data structure for the other two DFs. The class is dataframe because I coerced it to ```as.data.frame()```, but if I dont do that then when I put ```dput(df1 [1:3, 1:5])``` , i get the error ```subscript out of bounds``` – JVDeasyas123 Aug 11 '21 at 16:56
  • Your data has list-columns, so joins are likely to have problems. (Plus the `structure(.)` in your previous comment is incomplete, is has mis-matched parens.) – r2evans Aug 11 '21 at 16:59
  • If it looked like this, should it work? ```structure(list(a = "10", B= "23", C = "43"), row.names = "row 1", class = "data.frame")``` – JVDeasyas123 Aug 11 '21 at 17:03
  • *That* one looks normal. – r2evans Aug 11 '21 at 17:06
  • Okay, so I generated DF1 by using ```df1 <- df %>% select(A) %>% t() ``` because I essentially am just trying to generate a subsetted dataset with dplyr. How do I change this so it doesnt give me list-columns? – JVDeasyas123 Aug 11 '21 at 17:07
  • `t()` breaks `data.frame`s. Have you ever looked at the contents to see how the contents (`class` of each column) changes? Compare `str(iris)` with `str(t(iris))` to see what I mean. – r2evans Aug 11 '21 at 17:56

1 Answers1

0

In theory, your code works.

df1 <- data.frame(a = 1, b = 1)
df2 <- data.frame(b = 2, c = 2)
df3 <- data.frame(c = 3, d = 3)

dplyr::bind_rows(df1,df2, df3)

provides the following output

   a  b  c  d
1  1  1 NA NA
2 NA  2  2 NA
3 NA NA  3  3

The Error: Argument 1 must have names comes when you're not using a named structure. Can you confirm that your data frames are indeed data frames by checking them with

names(df1)

If you get NULL, then that is the problem.


As per your comment. If you use the t() function, it doesn't output a data frame, so you'll need to explicitly convert it to a data frame.

dta %>% t() %>% as.data.frame()

  • I did get null for the df1 and df2.... dput in the comments above....edit: null for all 3 dfs – JVDeasyas123 Aug 11 '21 at 16:57
  • with your last comment, make sure the last product is a data frame, not just a matrix/character. `df1 <- df %>% select(A) %>% t() %>% as.data.frame()`. The t() function outputs a matrix/vector – user16642972 Aug 11 '21 at 17:18
  • Now I think I have a different issue, which says ```Error: Can't combine `..1$A` and `..2$A` .``` I can ask this is a different question thread unless you think it is an easy answer. Thank you! – JVDeasyas123 Aug 11 '21 at 17:23
  • you'll need to make sure they're all consistent type. The safer thing is to convert 1$A to a character using `as.character()` and then binding rows. – user16642972 Aug 11 '21 at 17:59
  • Thank you, I foresee the problem being with multiple columns however. – JVDeasyas123 Aug 11 '21 at 18:01
  • then change them all to characters – user16642972 Aug 11 '21 at 18:40