Antje Antje - 1 year ago 68
R Question

How to assign table values to original data in R?

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:


With table I get:

> table(Obsevations)
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 Source

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)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download