急求解决 "duplicate 'row.names' are not allowed" in R plm package: There are no duplicates

Desperate to solve "duplicate 'row.names' are not allowed" in R plm package: There are no duplicates

我需要帮助解决以下代码末尾的错误。我不明白发生了什么。

library(plm)
    
sessionInfo()

# R version 4.0.3 (2020-10-10)
# Platform: x86_64-w64-mingw32/x64 (64-bit)
# Running under: Windows 10 x64 (build 19042)
# 
# Matrix products: default
# 
# locale:
# [1] LC_COLLATE=Spanish_Spain.1252  LC_CTYPE=Spanish_Spain.1252   
# [3] LC_MONETARY=Spanish_Spain.1252 LC_NUMERIC=C                  
# [5] LC_TIME=Spanish_Spain.1252    
# 
# attached base packages:
# [1] stats     graphics  grDevices utils     datasets  methods   base     
# 
# other attached packages:
# [1] plm_2.2-5
# 
# loaded via a namespace (and not attached):
#  [1] bdsmatrix_1.3-4  lattice_0.20-41  zoo_1.8-8        digest_0.6.27   
#  [5] lmtest_0.9-38    rbibutils_2.0    MASS_7.3-53      grid_4.0.3      
#  [9] nlme_3.1-149     Rdpack_2.1       miscTools_0.6-26 sandwich_3.0-0  
# [13] Formula_1.2-4    tools_4.0.3      tinytex_0.28     xfun_0.20       
# [17] compiler_4.0.3   gbRd_0.4-11      maxLik_1.4-6    
 
load("Errordug.RData")
    
class(gust)
# [1] "data.frame"

summary(gust)
#  ETreg                year        Country           ETregName        
#  Length:33          Min.   :2007   Length:33          Length:33         
#  Class :character   1st Qu.:2009   Class :character   Class :character  
#  Mode  :character   Median :2012   Mode  :character   Mode  :character  
#                     Mean   :2012                                        
#                     3rd Qu.:2015                                        
#                     Max.   :2017                                        
#        du                g          
#  Min.   :-4.4000   Min.   :-15.000  
#  1st Qu.:-1.2000   1st Qu.: -0.800  
#  Median : 0.0000   Median :  2.600  
#  Mean   : 0.2212   Mean   :  1.724  
#  3rd Qu.: 0.9000   3rd Qu.:  4.600  
#  Max.   : 8.0000   Max.   :  8.500  

dput(gust)

structure(list(ETreg = c("CY00", "CY00", "CY00", "CY00", "CY00", 
"CY00", "CY00", "CY00", "CY00", "CY00", "CY00", "EE00", "EE00", 
"EE00", "EE00", "EE00", "EE00", "EE00", "EE00", "EE00", "EE00", 
"EE00", "LU00", "LU00", "LU00", "LU00", "LU00", "LU00", "LU00", 
"LU00", "LU00", "LU00", "LU00"), year = c(2007, 2008, 2009, 2010, 
2011, 2012, 2013, 2014, 2015, 2016, 2017, 2007, 2008, 2009, 2010, 
2011, 2012, 2013, 2014, 2015, 2016, 2017, 2007, 2008, 2009, 2010, 
2011, 2012, 2013, 2014, 2015, 2016, 2017), Country = c("CY", 
"CY", "CY", "CY", "CY", "CY", "CY", "CY", "CY", "CY", "CY", "EE", 
"EE", "EE", "EE", "EE", "EE", "EE", "EE", "EE", "EE", "EE", "LU", 
"LU", "LU", "LU", "LU", "LU", "LU", "LU", "LU", "LU", "LU"), 
    ETregName = c("Kypros", "Kypros", "Kypros", "Kypros", "Kypros", 
    "Kypros", "Kypros", "Kypros", "Kypros", "Kypros", "Kypros", 
    "Eesti", "Eesti", "Eesti", "Eesti", "Eesti", "Eesti", "Eesti", 
    "Eesti", "Eesti", "Eesti", "Eesti", "Luxembourg", "Luxembourg", 
    "Luxembourg", "Luxembourg", "Luxembourg", "Luxembourg", "Luxembourg", 
    "Luxembourg", "Luxembourg", "Luxembourg", "Luxembourg"), 
    du = c(`CY00-2007` = -0.6, `CY00-2008` = -0.2, `CY00-2009` = 1.7, 
    `CY00-2010` = 0.899999999999999, `CY00-2011` = 1.6, `CY00-2012` = 3.9, 
    `CY00-2013` = 4.1, `CY00-2014` = 0.200000000000001, `CY00-2015` = -1.2, 
    `CY00-2016` = -2, `CY00-2017` = -1.8, `EE00-2007` = -1.3, 
    `EE00-2008` = 0.9, `EE00-2009` = 8, `EE00-2010` = 3.2, `EE00-2011` = -4.4, 
    `EE00-2012` = -2.3, `EE00-2013` = -1.4, `EE00-2014` = -1.2, 
    `EE00-2015` = -1.2, `EE00-2016` = 0.6, `EE00-2017` = -1, 
    `LU00-2007` = -0.600000000000001, `LU00-2008` = 1, `LU00-2009` = 0, 
    `LU00-2010` = -0.699999999999999, `LU00-2011` = 0.5, `LU00-2012` = 0.199999999999999, 
    `LU00-2013` = 0.7, `LU00-2014` = 0.100000000000001, `LU00-2015` = 0.8, 
    `LU00-2016` = -0.4, `LU00-2017` = -0.8), g = c(5.1, 3.6, 
    -2, 2, 0.4, -3.4, -6.6, -1.9, 3.4, 6.7, 4.4, 7.1, -2.9, -15, 
    4.1, 7.6, 2.6, 1.1, 2.8, 1.7, 2.1, 6.7, 8.5, -1.5, -4.6, 
    5, 2, -0.8, 3.6, 3.9, 4.6, 4.9, 1.7)), row.names = c(NA, 
-33L), class = "data.frame")

pdim(gust)

# Balanced Panel: n = 3, T = 11, N = 33

sum(is.na(gust))
# [1] 0

any(duplicated(rownames(pdata.frame(gust, , index = c("ETreg", "year"), row.names = TRUE))))
# [1] FALSE

rownames(pdata.frame(gust, , index = c("ETreg", "year"), row.names = TRUE))
#  [1] "CY00-2007" "CY00-2008" "CY00-2009" "CY00-2010" "CY00-2011"
#  [6] "CY00-2012" "CY00-2013" "CY00-2014" "CY00-2015" "CY00-2016"
# [11] "CY00-2017" "EE00-2007" "EE00-2008" "EE00-2009" "EE00-2010"
# [16] "EE00-2011" "EE00-2012" "EE00-2013" "EE00-2014" "EE00-2015"
# [21] "EE00-2016" "EE00-2017" "LU00-2007" "LU00-2008" "LU00-2009"
# [26] "LU00-2010" "LU00-2011" "LU00-2012" "LU00-2013" "LU00-2014"
# [31] "LU00-2015" "LU00-2016" "LU00-2017"

summary( plm(du ~ g, data = gust, index = c("ETreg", "year"), model="pooling") )
# Error in `.rowNamesDF<-`(x, value = value) : 
#   duplicate 'row.names' are not allowed
# Además: Warning messages:
# 1: In `[.data.frame`(index, as.numeric(rownames(mf)), ) :
#   NAs introducidos por coerción
# 2: In plm(du ~ g, data = gust, index = c("ETreg", "year"), model = "pooling") :
#   NAs introducidos por coerción
# 3: non-unique values when setting 'row.names': 

这看起来像是 plm 函数中的错误。 gust 中的 du 列已命名值;这导致 plm 崩溃。

您可以通过删除这些名称来解决此错误:

gust$du <- unname(gust$du)

在我这样做之后,我得到了成功的结果:

> summary(plm(du ~ g, data = gust, index = c("ETreg", "year"), model = "pooling"))
Pooling Model

Call:
plm(formula = du ~ g, data = gust, model = "pooling", index = c("ETreg", 
    "year"))

Balanced Panel: n = 3, T = 11, N = 33

Residuals:
    Min.  1st Qu.   Median  3rd Qu.     Max. 
-2.53175 -1.02819  0.27557  0.77953  3.84676 

Coefficients:
             Estimate Std. Error t-value  Pr(>|t|)    
(Intercept)  0.851158   0.263640  3.2285  0.002939 ** 
g           -0.365347   0.053228 -6.8639 1.079e-07 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Total Sum of Squares:    157.46
Residual Sum of Squares: 62.488
R-Squared:      0.60314
Adj. R-Squared: 0.59034
F-statistic: 47.1127 on 1 and 31 DF, p-value: 1.0794e-07

关于我如何到达检测到的问题的可重现的详细信息。

library(plm)
# I have needed eurostat package to create the dataframes g and u
library(eurostat)   
sessionInfo()
# R version 4.0.3 (2020-10-10)
# Platform: x86_64-w64-mingw32/x64 (64-bit)
# Running under: Windows 10 x64 (build 19042)
# Matrix products: default
# locale:
# [1] LC_COLLATE=Spanish_Spain.1252  LC_CTYPE=Spanish_Spain.1252   
# [3] LC_MONETARY=Spanish_Spain.1252 LC_NUMERIC=C                  
# [5] LC_TIME=Spanish_Spain.1252    
# attached base packages:
# [1] stats     graphics  grDevices utils     datasets  methods   base     
# other attached packages:
# [1] plm_2.2-5       eurostat_3.6.84
# loaded via a namespace (and not attached):
 # [1] tinytex_0.28       zoo_1.8-8          tidyselect_1.1.0  
 # [4] xfun_0.20          purrr_0.3.4        sf_0.9-7          
 # [7] lattice_0.20-41    vctrs_0.3.6        generics_0.1.0    
# [10] utf8_1.1.4         rlang_0.4.10       e1071_1.7-4       
# [13] pillar_1.4.7       glue_1.4.2         DBI_1.1.0         
# [16] sp_1.4-4           RColorBrewer_1.1-2 lifecycle_0.2.0   
# [19] plyr_1.8.6         stringr_1.4.0      bdsmatrix_1.3-4   
# [22] miscTools_0.6-26   gbRd_0.4-11        lmtest_0.9-38     
# [25] curl_4.3           class_7.3-17       fansi_0.4.1       
# [28] broom_0.7.3        Rcpp_1.0.5         KernSmooth_2.23-17
# [31] readr_1.4.0        backports_1.2.1    classInt_0.4-3    
# [34] jsonlite_1.7.2     countrycode_1.2.0  maxLik_1.4-6      
# [37] digest_0.6.27      hms_0.5.3          stringi_1.5.3     
# [40] dplyr_1.0.2        rbibutils_2.0      grid_4.0.3        
# [43] Rdpack_2.1         cli_2.2.0          tools_4.0.3       
# [46] sandwich_3.0-0     magrittr_2.0.1     tibble_3.0.4      
# [49] Formula_1.2-4      RefManageR_1.3.0   crayon_1.3.4      
# [52] tidyr_1.1.2        pkgconfig_2.0.3    ellipsis_0.3.1    
# [55] MASS_7.3-53        xml2_1.3.2         lubridate_1.7.9.2 
# [58] assertthat_0.2.1   httr_1.4.2         R6_2.5.0          
# [61] nlme_3.1-149       units_0.6-7        compiler_4.0.3    
ls()
# [1] "g" "u"
dput(g)
# structure(list(ETreg = c("CY00", "CY00", "CY00", "CY00", "CY00", 
# "CY00", "CY00", "CY00", "CY00", "CY00", "CY00", "EE00", "EE00", 
# "EE00", "EE00", "EE00", "EE00", "EE00", "EE00", "EE00", "EE00", 
# "EE00", "LU00", "LU00", "LU00", "LU00", "LU00", "LU00", "LU00", 
# "LU00", "LU00", "LU00", "LU00"), year = c(2007, 2008, 2009, 2010, 
# 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2007, 2008, 2009, 2010, 
# 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2007, 2008, 2009, 2010, 
# 2011, 2012, 2013, 2014, 2015, 2016, 2017), g = c(5.1, 3.6, -2, 
# 2, 0.4, -3.4, -6.6, -1.9, 3.4, 6.7, 4.4, 7.1, -2.9, -15, 4.1, 
# 7.6, 2.6, 1.1, 2.8, 1.7, 2.1, 6.7, 8.5, -1.5, -4.6, 5, 2, -0.8, 
# 3.6, 3.9, 4.6, 4.9, 1.7), ETregName = c("Kypros", "Kypros", "Kypros", 
# "Kypros", "Kypros", "Kypros", "Kypros", "Kypros", "Kypros", "Kypros", 
# "Kypros", "Eesti", "Eesti", "Eesti", "Eesti", "Eesti", "Eesti", 
# "Eesti", "Eesti", "Eesti", "Eesti", "Eesti", "Luxembourg", "Luxembourg", 
# "Luxembourg", "Luxembourg", "Luxembourg", "Luxembourg", "Luxembourg", 
# "Luxembourg", "Luxembourg", "Luxembourg", "Luxembourg"), Country = c("CY", 
# "CY", "CY", "CY", "CY", "CY", "CY", "CY", "CY", "CY", "CY", "EE", 
# "EE", "EE", "EE", "EE", "EE", "EE", "EE", "EE", "EE", "EE", "LU", 
# "LU", "LU", "LU", "LU", "LU", "LU", "LU", "LU", "LU", "LU")), row.names = c(NA, 
# -33L), class = c("tbl_df", "tbl", "data.frame"))
dput(u)
# structure(list(ETreg = c("CY", "CY", "CY", "CY", "CY", "CY", 
# "CY", "CY", "CY", "CY", "CY", "CY", "CY0", "CY0", "CY0", "CY0", 
# "CY0", "CY0", "CY0", "CY0", "CY0", "CY0", "CY0", "CY0", "CY00", 
# "CY00", "CY00", "CY00", "CY00", "CY00", "CY00", "CY00", "CY00", 
# "CY00", "CY00", "CY00", "EE", "EE", "EE", "EE", "EE", "EE", "EE", 
# "EE", "EE", "EE", "EE", "EE", "EE0", "EE0", "EE0", "EE0", "EE0", 
# "EE0", "EE0", "EE0", "EE0", "EE0", "EE0", "EE0", "EE00", "EE00", 
# "EE00", "EE00", "EE00", "EE00", "EE00", "EE00", "EE00", "EE00", 
# "EE00", "EE00", "LU", "LU", "LU", "LU", "LU", "LU", "LU", "LU", 
# "LU", "LU", "LU", "LU", "LU0", "LU0", "LU0", "LU0", "LU0", "LU0", 
# "LU0", "LU0", "LU0", "LU0", "LU0", "LU0", "LU00", "LU00", "LU00", 
# "LU00", "LU00", "LU00", "LU00", "LU00", "LU00", "LU00", "LU00", 
# "LU00"), year = c(2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 
# 2014, 2015, 2016, 2017, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 
# 2013, 2014, 2015, 2016, 2017, 2006, 2007, 2008, 2009, 2010, 2011, 
# 2012, 2013, 2014, 2015, 2016, 2017, 2006, 2007, 2008, 2009, 2010, 
# 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2006, 2007, 2008, 2009, 
# 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2006, 2007, 2008, 
# 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2006, 2007, 
# 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2006, 
# 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 
# 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 
# 2017), u = c(4.5, 3.9, 3.7, 5.4, 6.3, 7.9, 11.8, 15.9, 16.1, 
# 14.9, 12.9, 11.1, 4.5, 3.9, 3.7, 5.4, 6.3, 7.9, 11.8, 15.9, 16.1, 
# 14.9, 12.9, 11.1, 4.5, 3.9, 3.7, 5.4, 6.3, 7.9, 11.8, 15.9, 16.1, 
# 14.9, 12.9, 11.1, 5.9, 4.6, 5.5, 13.5, 16.7, 12.3, 10, 8.6, 7.4, 
# 6.2, 6.8, 5.8, 5.9, 4.6, 5.5, 13.5, 16.7, 12.3, 10, 8.6, 7.4, 
# 6.2, 6.8, 5.8, 5.9, 4.6, 5.5, 13.5, 16.7, 12.3, 10, 8.6, 7.4, 
# 6.2, 6.8, 5.8, 4.7, 4.1, 5.1, 5.1, 4.4, 4.9, 5.1, 5.8, 5.9, 6.7, 
# 6.3, 5.5, 4.7, 4.1, 5.1, 5.1, 4.4, 4.9, 5.1, 5.8, 5.9, 6.7, 6.3, 
# 5.5, 4.7, 4.1, 5.1, 5.1, 4.4, 4.9, 5.1, 5.8, 5.9, 6.7, 6.3, 5.5
# )), row.names = c(NA, -108L), class = c("tbl_df", "tbl", "data.frame"
# ))
u.pd <- pdata.frame(u,index = c("ETreg", "year"))
u.pd$du <- diff(u.pd$u)      
class(u.pd$du)
# [1] "pseries" "numeric"
u$du <- u.pd$du  
head(u)
# A tibble: 6 x 4
  # ETreg  year     u du       
  # <chr<dbl<dbl<pseries>
# 1 CY     2006   4.5   NA     
# 2 CY     2007   3.9 -0.6     
# 3 CY     2008   3.7 -0.2     
# 4 CY     2009   5.4  1.7     
# 5 CY     2010   6.3  0.9     
# 6 CY     2011   7.9  1.6     
class(u$du)
# [1] "pseries" "numeric"
gust <- merge(g, u, by=c("ETreg", "year"))  
gust <- gust[ c("ETreg","year", "Country", "ETregName", "u", "du", "g" )]
class(gust$du)
# [1] "numeric"
dput(gust)
# structure(list(ETreg = c("CY00", "CY00", "CY00", "CY00", "CY00", 
# "CY00", "CY00", "CY00", "CY00", "CY00", "CY00", "EE00", "EE00", 
# "EE00", "EE00", "EE00", "EE00", "EE00", "EE00", "EE00", "EE00", 
# "EE00", "LU00", "LU00", "LU00", "LU00", "LU00", "LU00", "LU00", 
# "LU00", "LU00", "LU00", "LU00"), year = c(2007, 2008, 2009, 2010, 
# 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2007, 2008, 2009, 2010, 
# 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2007, 2008, 2009, 2010, 
# 2011, 2012, 2013, 2014, 2015, 2016, 2017), Country = c("CY", 
# "CY", "CY", "CY", "CY", "CY", "CY", "CY", "CY", "CY", "CY", "EE", 
# "EE", "EE", "EE", "EE", "EE", "EE", "EE", "EE", "EE", "EE", "LU", 
# "LU", "LU", "LU", "LU", "LU", "LU", "LU", "LU", "LU", "LU"), 
    # ETregName = c("Kypros", "Kypros", "Kypros", "Kypros", "Kypros", 
    # "Kypros", "Kypros", "Kypros", "Kypros", "Kypros", "Kypros", 
    # "Eesti", "Eesti", "Eesti", "Eesti", "Eesti", "Eesti", "Eesti", 
    # "Eesti", "Eesti", "Eesti", "Eesti", "Luxembourg", "Luxembourg", 
    # "Luxembourg", "Luxembourg", "Luxembourg", "Luxembourg", "Luxembourg", 
    # "Luxembourg", "Luxembourg", "Luxembourg", "Luxembourg"), 
    # u = c(3.9, 3.7, 5.4, 6.3, 7.9, 11.8, 15.9, 16.1, 14.9, 12.9, 
    # 11.1, 4.6, 5.5, 13.5, 16.7, 12.3, 10, 8.6, 7.4, 6.2, 6.8, 
    # 5.8, 4.1, 5.1, 5.1, 4.4, 4.9, 5.1, 5.8, 5.9, 6.7, 6.3, 5.5
    # ), du = c(`CY00-2007` = -0.6, `CY00-2008` = -0.2, `CY00-2009` = 1.7, 
    # `CY00-2010` = 0.899999999999999, `CY00-2011` = 1.6, `CY00-2012` = 3.9, 
    # `CY00-2013` = 4.1, `CY00-2014` = 0.200000000000001, `CY00-2015` = -1.2, 
    # `CY00-2016` = -2, `CY00-2017` = -1.8, `EE00-2007` = -1.3, 
    # `EE00-2008` = 0.9, `EE00-2009` = 8, `EE00-2010` = 3.2, `EE00-2011` = -4.4, 
    # `EE00-2012` = -2.3, `EE00-2013` = -1.4, `EE00-2014` = -1.2, 
    # `EE00-2015` = -1.2, `EE00-2016` = 0.6, `EE00-2017` = -1, 
    # `LU00-2007` = -0.600000000000001, `LU00-2008` = 1, `LU00-2009` = 0, 
    # `LU00-2010` = -0.699999999999999, `LU00-2011` = 0.5, `LU00-2012` = 0.199999999999999, 
    # `LU00-2013` = 0.7, `LU00-2014` = 0.100000000000001, `LU00-2015` = 0.8, 
    # `LU00-2016` = -0.4, `LU00-2017` = -0.8), g = c(5.1, 3.6, 
    # -2, 2, 0.4, -3.4, -6.6, -1.9, 3.4, 6.7, 4.4, 7.1, -2.9, -15, 
    # 4.1, 7.6, 2.6, 1.1, 2.8, 1.7, 2.1, 6.7, 8.5, -1.5, -4.6, 
    # 5, 2, -0.8, 3.6, 3.9, 4.6, 4.9, 1.7)), row.names = c(NA, 
# -33L), class = "data.frame")
pdim(gust)
# Balanced Panel: n = 3, T = 11, N = 33
sum(is.na(gust))
# [1] 0
any(duplicated(rownames(pdata.frame(gust, , index = c("ETreg", "year"), row.names = TRUE))))
# [1] FALSE
rownames(pdata.frame(gust, , index = c("ETreg", "year"), row.names = TRUE))
 # [1] "CY00-2007" "CY00-2008" "CY00-2009" "CY00-2010" "CY00-2011"
 # [6] "CY00-2012" "CY00-2013" "CY00-2014" "CY00-2015" "CY00-2016"
# [11] "CY00-2017" "EE00-2007" "EE00-2008" "EE00-2009" "EE00-2010"
# [16] "EE00-2011" "EE00-2012" "EE00-2013" "EE00-2014" "EE00-2015"
# [21] "EE00-2016" "EE00-2017" "LU00-2007" "LU00-2008" "LU00-2009"
# [26] "LU00-2010" "LU00-2011" "LU00-2012" "LU00-2013" "LU00-2014"
# [31] "LU00-2015" "LU00-2016" "LU00-2017"
str(gust$g)
 # num [1:33] 5.1 3.6 -2 2 0.4 -3.4 -6.6 -1.9 3.4 6.7 ...
str(gust$du)
 # Named num [1:33] -0.6 -0.2 1.7 0.9 1.6 ...
 # - attr(*, "names")= chr [1:33] "CY00-2007" "CY00-2008" "CY00-2009" "CY00-2010" ...
summary( plm(du ~ g, data = gust, index = c("ETreg", "year"), model="pooling") )        
# Error in `.rowNamesDF<-`(x, value = value) : 
  # duplicate 'row.names' are not allowed
# Además: Warning messages:
# 1: In `[.data.frame`(index, as.numeric(rownames(mf)), ) :
  # NAs introducidos por coerción
# 2: In plm(du ~ g, data = gust, index = c("ETreg", "year"), model = "pooling") :
  # NAs introducidos por coerción
# 3: non-unique values when setting 'row.names':  
# Timing stopped at: 0.71 0.14 7.11
gust$du <- unname(gust$du)      
summary( plm(du ~ g, data = gust, index = c("ETreg", "year"), model="pooling") )
# Pooling Model
# Call:
# plm(formula = du ~ g, data = gust, model = "pooling", index = c("ETreg", 
    # "year"))
# Balanced Panel: n = 3, T = 11, N = 33
# Residuals:
    # Min.  1st Qu.   Median  3rd Qu.     Max. 
# -2.53175 -1.02819  0.27557  0.77953  3.84676 
# Coefficients:
             # Estimate Std. Error t-value  Pr(>|t|)    
# (Intercept)  0.851158   0.263640  3.2285  0.002939 ** 
# g           -0.365347   0.053228 -6.8639 1.079e-07 ***
# ---
# Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
# Total Sum of Squares:    157.46
# Residual Sum of Squares: 62.488
# R-Squared:      0.60314
# Adj. R-Squared: 0.59034
# F-statistic: 47.1127 on 1 and 31 DF, p-value: 1.0794e-07

这是一个简短的例子,说明了数据框和 tibble(tibble 3.0.4)在添加具有 names 属性的向量的行为方面的区别:

library(tibble)
a <- 1:5
df <- data.frame(a, a * 2)
tb <- tibble(df)

b <- 6:10
names(b) <- letters[1:5]

df$b <- b
tb$b <- b

lapply(df, names) # data frame's columns do not have names
#> $a
#> NULL
#> 
#> $a...2
#> NULL
#> 
#> $b
#> NULL
lapply(tb, names) # tibble's column b preserved names
#> $a
#> NULL
#> 
#> $a...2
#> NULL
#> 
#> $b
#> [1] "a" "b" "c" "d" "e"

df_tb <- as.data.frame(tb)
lapply(df_tb, names) # data frame's column b has names
#> $a
#> NULL
#> 
#> $a...2
#> NULL
#> 
#> $b
#> [1] "a" "b" "c" "d" "e"