Antje - 8 months ago 33

R Question

I'm quiet new in working with R, so sorry for any stupid question.

Usually I have to deal with really big data sets.

I want to have the number of observations for each observation, without making the original dataset smaller.

I have a very small example for my problem:

`Observations<-c("A1","A2","B1","B3","B3","C1","C1","C1","C1","C2","C3","C3","C3")`

With table I get:

`> table(Obsevations)`

Observations

A1 A2 B1 B3 C1 C2 C3

1 1 1 2 4 1 3

But what I actually want is:

`A1 A2 B1 B3 B3 C1 C1 C1 C1 C2 C3 C3 C3`

1 1 1 2 2 4 4 4 4 1 3 3 3

Is there an elegant and fast way to do this in big data sets without any loops?

Thanks in advance!

Answer

We need `rep`

here

```
tbl <- table(Observations)
rep(tbl, tbl)
# A1 A2 B1 B3 B3 C1 C1 C1 C1 C2 C3 C3 C3
# 1 1 1 2 2 4 4 4 4 1 3 3 3
```

Based on the example, another option is `ave`

```
ave(seq_along(Observations), Observations, FUN = length)
```

Source (Stackoverflow)