为什么我不能在R中绑定这些数据帧?

时间:2016-04-19 15:21:51

标签: r dataframe row rbind

我正在尝试在R中绑定两个数据帧但是仍然得到一个'重复'row.names'不允许'错误。它们是两个数据帧,每行数百行,列数相同。我尝试在使用rbind之前定义行名称,但我仍然得到错误。我正在使用这个:

row.names(sons) <- make.names(1:100, unique=TRUE)
row.names(sons2)<-make.names(101:200, unique = TRUE)
sons3 <- rbind(sons, sons2)

我的数据来自英国公司大楼的API。数组中的条目如下所示:

description_identifier                  kind                        title company_type
X1           dissolved-on searchresults#company THIS IS A COMPANY NAME          ltd
                self                                snippet date_of_cessation
X1 /company/08733334 Unit 2, 41 A Road, London, E8  2QH        2015-11-10
                                description date_of_creation company_number company_status
X1 08730034 - Dissolved on 10 November 2015       2013-10-14       08730034      dissolved

如何绑定这些数据框?

编辑:根据要求,dput(head(sons))的输出是

structure(list(title = c("SON AND SONS PRODUCTIONS LTD", "SJ AND SONS LTD", 
"DW AND SONS LTD", "PJ AND SONS LTD", "ND AND SONS LTD", "TF AND SONS LTD"
), company_type = c("ltd", "ltd", "ltd", "ltd", "ltd", "ltd"), 
    links = structure(list(self = c("/company/08730034", "/company/08902692", 
    "/company/09211826", "/company/09496249", "/company/09669572", 
    "/company/NI633653")), .Names = "self", row.names = c(NA, 
    6L), class = "data.frame"), snippet = c("Unit E1, 41 Dace Road, London, E3  2NG", 
    "17 Poundgate Lane, Westwood Heath, Coventry, CV4 8HJ", "60, Hawkins Road, Folkestone, CT19 4JA", 
    "Dalton House, 9 Dalton Square, Lancaster, LA1 1WD", "19 Barnston Lane, Moreton, Wirral, CH46 7TN", 
    "C/O Rpb Chartered Accountants, 22, St. Colmans Park, Newry, County Down, BT34 2BX"
    ), date_of_cessation = c("2015-11-10", "2015-09-29", "2015-03-17", 
    NA, NA, NA), description = c("08730034 - Dissolved on 10 November 2015", 
    "08902692 - Dissolved on 29 September 2015", "09211826 - Dissolved on 17 March 2015", 
    "09496249 - Incorporated on 18 March 2015", "09669572 - Incorporated on  3 July 2015", 
    "NI633653 - Incorporated on 16 September 2015"), date_of_creation = c("2013-10-14", 
    "2014-02-19", "2014-09-10", "2015-03-18", "2015-07-03", "2015-09-16"
    ), company_number = c("08730034", "08902692", "09211826", 
    "09496249", "09669572", "NI633653"), company_status = c("dissolved", 
    "dissolved", "dissolved", "active", "active", "active")), .Names = c("title", 
"company_type", "links", "snippet", "date_of_cessation", "description", 
"date_of_creation", "company_number", "company_status"), row.names = c("X1", 
"X2", "X3", "X4", "X5", "X6"), class = "data.frame")

和dput(head(sons2))它是

structure(list(title = c("SON AND SONS PRODUCTIONS LTD", "SJ AND SONS LTD", 
"DW AND SONS LTD", "PJ AND SONS LTD", "ND AND SONS LTD", "TF AND SONS LTD"
), company_type = c("ltd", "ltd", "ltd", "ltd", "ltd", "ltd"), 
    links = structure(list(self = c("/company/08730034", "/company/08902692", 
    "/company/09211826", "/company/09496249", "/company/09669572", 
    "/company/NI633653")), .Names = "self", row.names = c(NA, 
    6L), class = "data.frame"), snippet = c("Unit E1, 41 Dace Road, London, E3  2NG", 
    "17 Poundgate Lane, Westwood Heath, Coventry, CV4 8HJ", "60, Hawkins Road, Folkestone, CT19 4JA", 
    "Dalton House, 9 Dalton Square, Lancaster, LA1 1WD", "19 Barnston Lane, Moreton, Wirral, CH46 7TN", 
    "C/O Rpb Chartered Accountants, 22, St. Colmans Park, Newry, County Down, BT34 2BX"
    ), date_of_cessation = c("2015-11-10", "2015-09-29", "2015-03-17", 
    NA, NA, NA), description = c("08730034 - Dissolved on 10 November 2015", 
    "08902692 - Dissolved on 29 September 2015", "09211826 - Dissolved on 17 March 2015", 
    "09496249 - Incorporated on 18 March 2015", "09669572 - Incorporated on  3 July 2015", 
    "NI633653 - Incorporated on 16 September 2015"), date_of_creation = c("2013-10-14", 
    "2014-02-19", "2014-09-10", "2015-03-18", "2015-07-03", "2015-09-16"
    ), company_number = c("08730034", "08902692", "09211826", 
    "09496249", "09669572", "NI633653"), company_status = c("dissolved", 
    "dissolved", "dissolved", "active", "active", "active")), .Names = c("title", 
"company_type", "links", "snippet", "date_of_cessation", "description", 
"date_of_creation", "company_number", "company_status"), row.names = c("X101", 
"X102", "X103", "X104", "X105", "X106"), class = "data.frame")

1 个答案:

答案 0 :(得分:1)

看来你的links变量存储为data.frame,这是问题的核心。要解决此问题,请使用以下命令:

sons$links <- as.character(unlist(sons$links))
sons2$links <- as.character(unlist(sons2$links))

您可能还需要完全删除rownames:

row.names(sons) <- NULL
row.names(sons2)<- NULL
sons3 <- rbind(sons, sons2)

如果要保留它们,请先将它们保存为矢量。另外,