Antje
Antje

Reputation: 11

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:

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!

Upvotes: 0

Views: 54

Answers (1)

akrun
akrun

Reputation: 887118

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)

Upvotes: 1

Related Questions