0

R Proficiency : Beginner

I'm trying to combine together dataframes from github issues api by following pages. But when I try to combine the last dataframe with the next one it gives me duplicate rownames error.

res <- GET(url,token,query = params)
resDF <- fromJSON(httr::content(res, as = "text"))
while (grepl("next", res$headers$link) == 'TRUE')
{
  next_url = ifelse(grepl("prev", res$headers$link) == 'TRUE',
           str_match(res$headers$link, 'rel="prev", <(.*)>; rel="next"')[1,2]
           ,
           str_match(res$headers$link, '<(.*)>; rel="next"')[1,2]
    )
  #print(next_url) 
  next_res <- GET(next_url,token)
  next_resDF <- fromJSON(httr::content(res, as = "text"))
  resDF <- rbind(resDF, next_resDF)
}

Complete Error:

    Warning message:
    “non-unique values when setting 'row.names': ‘1’, ‘10’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’”
Error in `.rowNamesDF<-`(x, value = value): duplicate 'row.names' are not allowed
Traceback:

1. rbind(resDF, fromJSON(httr::content(res, as = "text")))
2. rbind(deparse.level, ...)
3. `rownames<-`(`*tmp*`, value = `*vtmp*`)
4. `row.names<-`(`*tmp*`, value = value)
5. `row.names<-.data.frame`(`*tmp*`, value = value)
6. `.rowNamesDF<-`(x, value = value)
7. stop("duplicate 'row.names' are not allowed")

I understand the error because with every api call the returned resDF will have rownames starting from 1

To handle this I tried increment the rownames of new DF by a counter

rownames(next_resDF) <- c("11", "12", "13", "14", "15"...)

Haven't been able to achieve this

Appreciate any helpful pointers

ankitj
  • 335
  • 4
  • 13
  • Does using `bind_rows` from `dplyr` work instead of `rbind`? – Sonny Feb 28 '19 at 17:47
  • 1
    It will be quite a bit more efficient to [make a list of data frames](https://stackoverflow.com/a/24376207/903061) and then do a single `resDF = bind_rows(list_of_data_frames)` at the end. – Gregor Thomas Feb 28 '19 at 17:50
  • 1
    Also, worth noting the difference between a warning and an error. An error stops your code from working because there is a problem R can't get around. A warning lets you know that something *might* be wrong. You are getting a warning, not an error. The code executes completely, and you need to see if the result is okay. If you don't care about the row names, it's probably just fine. – Gregor Thomas Feb 28 '19 at 17:54
  • Lastly, I think you have a syntax error. `rbind(resDF, next_resDF)()` will try to use the `rbind()` result as a function. Delete the extra `()` at the end of the line. – Gregor Thomas Feb 28 '19 at 17:54
  • p.s., I can't replicate your problem. If we do `x = data.frame(col = 1); row.names(x) = "a"`, then even though the row names are the same `rbind(x, x)` works just fine, no warning, no error. Could you give us a way to reproduce the problem? – Gregor Thomas Feb 28 '19 at 18:41

0 Answers0