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

时间:2017-09-20 16:34:55

标签: r dataframe

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

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

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)。这是否意味着它只能从输入对象生成名称?

1 个答案:

答案 0 :(得分:0)

数据的类是矩阵:

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() 更改为 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