R 程序,?count,将 "freq" 重命名为其他名称

R program, ?count, rename "freq" to something else

我正在研究这个 webpage,不知道如何将 freq 重命名为其他名称,比如说 number of times imbibed

这里是dput

structure(list(name = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 
2L), .Label = c("Bill", "Llib"), class = "factor"), drink = structure(c(2L, 
3L, 1L, 4L, 2L, 3L, 1L, 4L), .Label = c("cocoa", "coffee", "tea", 
"water"), class = "factor"), cost = 1:8), .Names = c("name", 
"drink", "cost"), row.names = c(NA, -8L), class = "data.frame")

这是带输出的工作代码。同样,我想重命名 freq 列。谢谢!

library(plyr)

bevs$cost <- as.integer(bevs$cost)
count(bevs, "name")

输出

  name freq
1 Bill    4
2 Llib    4

count()函数returns一个data.frame。只需将其重命名为任何其他 data.frame:

counts <- count(bevs, "name")
names(counts)[which(names(counts) == "freq")] <- "number of times imbibed"
print(counts)
#   name number of times imbibed
# 1 Bill                       4
# 2 Llib                       4

您要这样做吗?

counts <- count(bevs, "name")
names(counts) <- c("name", "number of times imbibed")
counts