user14892865
user14892865

Reputation:

Aggregate single column in R

I have some data with only one variable, as following:

dd <- data.frame (
  x=sample (1:10, 100, T)
)

I want to aggregate, for example count each occurance, but only with base package's functions

dd |>
  transform(y=1) |>
  do(aggregate(y~x, data=., FUN = \(x) length(x)))

is there any better solution?

Upvotes: 0

Views: 34

Answers (1)

Karthik S
Karthik S

Reputation: 11584

Will this work:

as.data.frame(table(dd))
   dd Freq
1   1   11
2   2   10
3   3   13
4   4    8
5   5   13
6   6    9
7   7    6
8   8   10
9   9   10
10 10   10

Upvotes: 1

Related Questions