R程序,计数,重命名" freq"别的东西

时间:2015-07-19 21:11:23

标签: r count aggregate

我正在研究这个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

2 个答案:

答案 0 :(得分:2)

你想这样做吗?

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

答案 1 :(得分:1)

count()函数返回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