1

I have a data frame m

A   2
B   3
C   4

and I want to create a data frame like

A 1
A 2
B 1
B 2
B 3
C 1
C 2
C 3
C 4

Any help? Thanks a lot in advance

4
  • 2
    That doesn't look like a matrix. Do you mean a data frame? A matrix can only hold one of numeric or characters. Aug 22, 2011 at 20:17
  • 1
    Also, in future, don't paste something you think is sufficient to describe your data. Use dput(foo) instead, where foo is your data object. That way we can reproduce exactly the object you are using. Aug 22, 2011 at 20:21
  • 1
    -1 There are several problems here. 1) Your sample data and results don't look like a matrix. 2) Your edit has fundamentally changed the question, after three people have already answered.
    – Andrie
    Aug 22, 2011 at 20:28
  • Sorry for changing the question. Newbie. Wont happen again.
    – ECII
    Aug 22, 2011 at 20:31

4 Answers 4

5

Your original question can be answered by:

text <- LETTERS[1:3]
n <- 2:4

rep(text, times=n)

[1] "A" "A" "B" "B" "B" "C" "C" "C" "C"

Your new question is quite different:

df <- data.frame(
  text <- LETTERS[1:3],
  n <- 2:4
)  

data.frame(
    text = rep(df$text, times=df$n),
    seq  = sequence(df$n)
)

  text seq
1    A   1
2    A   2
3    B   1
4    B   2
5    B   3
6    C   1
7    C   2
8    C   3
9    C   4
3

rep accepts vectors. Try this:

dat <- data.frame(V1 = letters[1:3], V2 = 2:4)

rep(dat[, 1], dat[, 2])


> rep(dat[, 1], dat[, 2])
[1] a a b b b c c c c
3

Assuming m is a data frame:

m <- data.frame(V1 = LETTERS[1:3], V2 = 2:4, stringsAsFactors = FALSE)

This will do what you want:

with(m, rep(V1, times = V2))

e.g.

> with(m, rep(V1, times = V2))
[1] "A" "A" "B" "B" "B" "C" "C" "C" "C"

Edit: To address the edit made by the OP, try the following:

with(m, data.frame(X1 = rep(V1, times = V2), 
                   X2 = unlist(lapply(V2, seq_len))))

Which produces:

>  with(m, data.frame(X1 = rep(V1, times = V2), 
+                        X2 = unlist(lapply(V2, seq_len))))
  X1 X2
1  A  1
2  A  2
3  B  1
4  B  2
5  B  3
6  C  1
7  C  2
8  C  3
9  C  4

Or more succinctly via sequence() — as per @Andrie's Answer (which I also keep forgetting about):

with(m, data.frame(X1 = rep(V1, times = V2), X2 = sequence(V2)))
2

@Andrie's answer is the only one so far that answers your new question. There may be a better way to do this but:

m <- data.frame(V1 = LETTERS[1:3], V2 = 2:4, stringsAsFactors = FALSE)
library(plyr)
ddply(m,"V1",function(x) data.frame(V2=seq(x[,2])))
2
  • Did you mean library(plyr)? Ah, I see that library(reshape) depends on plyr so this gets loaded correctly.
    – Andrie
    Aug 22, 2011 at 20:39
  • 1
    A better way to use plyr is to make use of adply instead of ddply, as follows: adply(m,1,function(x) data.frame(V1=x[,1],V2=seq(x[,2])))[,-1]. This means you don't have to construct your m$row column.
    – Andrie
    Aug 22, 2011 at 20:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.