如何在R中将数据帧与顺序行名结合在一起?

时间:2019-02-28 17:44:08

标签: r

R熟练程度:初学者

我正在尝试通过以下页面将来自github issue api的数据帧组合在一起。 但是,当我尝试将最后一个数据框与下一个数据框合并时,会出现重复的行名错误。

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)
}

完全错误:

    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")

我理解该错误,因为每次api调用返回的resDF的行名都将从1开始

为解决这个问题,我尝试通过计数器增加新DF的行名

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

无法做到这一点

赞赏所有有用的指针

0 个答案:

没有答案