为什么 as.data.frame 忽略 col.names = vector

2024-01-05

我顽固地尝试在一行中创建一个具有命名行和列的数据框。我意识到我可以通过使用 colnames(forecast) 轻松地做到这一点,但想简化一下,如果只是为了证明我可以做到这一点。

以下不起作用——没有错误,但未设置列名

forecast <- as.data.frame(cbind(c(1, 32, 60, 91, 121, 152,
                                182, 213, 244, 274, 305, 335),
                                c(31, 59, 90, 120, 151, 181,
                                212, 243, 273, 304, 334, 365),
                                as.vector(rep(0, times=12))),
                                row.names = c("Jan", "Feb", "Mar", "Apr", "May", "Jun", 
                                               "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"),
                                col.names = c("Start", "End", "forecast"))


> forecast
     V1  V2 V3
Jan   1  31  0
Feb  32  59  0
Mar  60  90  0
Apr  91 120  0
May 121 151  0
Jun 152 181  0
Jul 182 212  0
Aug 213 243  0
Sep 244 273  0
Oct 274 304  0
Nov 305 334  0
Dec 335 365  0

以下确实按预期工作。

forecast <- setNames(data.frame(cbind(c(1, 32, 60, 91, 121, 152,
                                      182, 213, 244, 274, 305, 335),
                                      c(31, 59, 90, 120, 151, 181,
                                      212, 243, 273, 304, 334, 365),
                                      as.vector(rep(0, times=12))),
                                      row.names = c("Jan", "Feb", "Mar", "Apr", "May", "Jun", 
                                                "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")),
                                      c("Start", "End", "forecast"))

> forecast
    Start End forecast
Jan     1  31        0
Feb    32  59        0
Mar    60  90        0
Apr    91 120        0
May   121 151        0
Jun   152 181        0
Jul   182 212        0
Aug   213 243        0
Sep   244 273        0
Oct   274 304        0
Nov   305 334        0
Dec   335 365        0

必须嵌套在另一个函数中似乎很奇怪。在 as.data.frame 文档中,它说使用 col.names = names(x)。这是否意味着它只能从输入对象生成名称?


数据的类别是矩阵:

d <- cbind(c(1, 32, 60, 91, 121, 152,
                            182, 213, 244, 274, 305, 335),
                            c(31, 59, 90, 120, 151, 181,
                            212, 243, 273, 304, 334, 365),
                            as.vector(rep(0, times=12)))
is.matrix(d)
# TRUE

如果你检查方法as.data.frame使用时,如下:

## S3 method for class 'character'
as.data.frame(x, ...,
              stringsAsFactors = default.stringsAsFactors())

## S3 method for class 'list'
as.data.frame(x, row.names = NULL, optional = FALSE, ...,
              cut.names = FALSE, col.names = names(x), fix.empty.names = TRUE,
              stringsAsFactors = default.stringsAsFactors())

## S3 method for class 'matrix'
as.data.frame(x, row.names = NULL, optional = FALSE,
              make.names = TRUE, ...,
              stringsAsFactors = default.stringsAsFactors())

你可以看到这个选项col.names只能在命名列表对象中使用。您可以简单地从cbind() to list()使其发挥作用:

forecast <- as.data.frame(list(c(1, 32, 60, 91, 121, 152,
                                182, 213, 244, 274, 305, 335),
                                c(31, 59, 90, 120, 151, 181,
                                212, 243, 273, 304, 334, 365),
                                as.vector(rep(0, times=12))),
                                row.names = c("Jan", "Feb", "Mar", "Apr", "May", "Jun", 
                                               "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"),
                                col.names = c("Start", "End", "forecast"))

#     Start End forecast
# Jan     1  31        0
# Feb    32  59        0
# Mar    60  90        0
# Apr    91 120        0
# May   121 151        0
# Jun   152 181        0
# Jul   182 212        0
# Aug   213 243        0
# Sep   244 273        0
# Oct   274 304        0
# Nov   305 334        0
# Dec   335 365        0
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

为什么 as.data.frame 忽略 col.names = vector 的相关文章

随机推荐