Rhonda
Rhonda

Reputation: 1721

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

I am studying this webpage, and cannot figure out how to rename freq to something else, say number of times imbibed

Here is 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")

And this is working code with output. Again, I'd like to rename the freq column. Thanks!

library(plyr)

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

Output

  name freq
1 Bill    4
2 Llib    4

Upvotes: 1

Views: 2541

Answers (2)

dommer
dommer

Reputation: 19810

Are you trying to do this?

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

Upvotes: 2

maccruiskeen
maccruiskeen

Reputation: 2818

The count() function returns a data.frame. Just rename it like any other 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

Upvotes: 1

Related Questions