1

I have a dataframe with 2 columns

>mydata <- data.frame(Obj = c(1,2,3,2), Count = c(2,3,1,4))
>mydata
  Obj Count
1   1     2
2   2     3
3   3     1
4   2     4

I want to split the Obj column based on count to get the atomic objects like (1, 1, 2, 2, 2, 3, 2, 2, 2, 2) Yeah that is reverse of the table function. Is there any function for doing this in R?

P.S: A simple for loop can do the trick, but I feel it is always good to use inbuilt efficient functions.

4

2 に答える 2

2

あなたが探してrepいるCountのは、時代の議論です

with(mydata, rep(Obj, Count))

またはrep.int、これはわずかに高速な実装ですrep(x, times)

with(mydata, rep.int(Obj,  Count))
于 2013-02-20T02:58:51.720 に答える
2

試す

rep(mydata$Obj,mydata$Count)
于 2013-02-20T03:01:56.100 に答える