无法将列名设置为数据框的子集

Unable to set column names to a subset of a dataframe

我运行下面的代码,p是加载的dataframe。

a <- sort(table(p$Title))
a1 <- as.data.frame(a)
tail(a1, 7)

                     a
Maths               732
Science             737
Physics             737
Chemistry           776
Social Science      905
null              57374
                  88117

我想对上面的数据帧结果做一些操作。我想将列名添加到数据框中。我尝试了 colnames 函数。

colnames(a1) <- c("category", "count")

我收到以下错误:

Error in `colnames<-`(`*tmp*`, value = c("category", "count")) : 
    attempt to set 'colnames' on an object with less than two dimensions

求推荐。

正如我在对您的问题的评论中所说,类别是行名。一个可重现的例子:

# create dataframe p
x <- c("Maths","Science","Physics","Chemistry","Social Science","Languages","Economics","History")
set.seed(1)
p <- data.frame(title=sample(x, 100, replace=TRUE), y="some arbitrary value")

# create the data.frame as you did
a <- sort(table(p$title))
a1 <- as.data.frame(a)

生成的数据帧:

> a1
                a
Social Science  6
Maths           9
History        10
Science        11
Physics        12
Languages      15
Economics      17
Chemistry      20

查看数据框的维度 a1,您会得到:

> dim(a1)
[1] 8 1

这意味着您的数据框有 8 行和 1 列。因此,尝试将两个列名分配给 a1 数据框将导致错误。

您可以通过两种方式解决您的问题:

1:仅分配 1 个列名 colnames(a1) <- c("count")

2:将行名转换为类别列,然后分配列名:

a1$category <- row.names(a1)
colnames(a1) <- c("count","category")

生成的数据帧:

> a1
               count       category
Social Science     6 Social Science
Maths              9          Maths
History           10        History
Science           11        Science
Physics           12        Physics
Languages         15      Languages
Economics         17      Economics
Chemistry         20      Chemistry

您可以使用 rownames(a1) <- NULL 删除行名。这给出:

> a1

  count       category
1     6 Social Science
2     9          Maths
3    10        History
4    11        Science
5    12        Physics
6    15      Languages
7    17      Economics
8    20      Chemistry