0

I have the following elementary issue in R.

I have a for (k in 1:x){...} cycle which produces numerical vectors whose length depends on k. For each value of k I produce a single numerical vector.

I would like to collect them as rows of a data frame in R, if possible. In other words, I would like to introduce a data frame data s.t.

    for (k in 1:x) {
            data[k,] <- ...      
                     }

where the dots represent the command producing the vector with length depending on k.

Unfortunately, as far as I know, the length of the rows of a dataframe in R is constant, as it is a list of vectors of equal length. I have already tried to complete each row with a suitable number of zeroes to arrive at a constant length (in this case equal to x). I would like to work "dynamically", instead.

I do not think that this issue is equivalent to merge vectors of different lengths in a dataframe; due to the if cycle, only 1 vector is known at each step.

Edit

A very easy example of what I mean. For each k, I would like to write the vector whose components are 1,2,...,k and store it as kth row of the dataframe data. In the above setting, I would write

 for (k in 1:x) {
                data[k,] <- seq(1,k,1)     
                         }

As the length of seq(1,k,1) depends on k the code does not work.

A5C1D2H2I1M1N2O1R2T1
  • 190,393
  • 28
  • 405
  • 485
Avitus
  • 734
  • 2
  • 14
  • 27
  • Could you please give a simple example of what you want to achieve? The wording of the question is a bit confusing, and I am not sure what you mean by the `if` cycle in : `I do not think that this issue is equivalent to merge vectors of different lengths in a dataframe; due to the "if" cycle, only 1 vector is known at each step`. Are you referring to some `ifelse()` statement? I don't see any in your code. – Mayou Aug 20 '13 at 12:48
  • Ok, I will edit the question accordingly! Thanks for the feedback. – Avitus Aug 20 '13 at 12:49
  • Also http://stackoverflow.com/questions/15396188/can-you-use-rbind-fill-without-having-it-fill-in-nas, http://stackoverflow.com/questions/15486693/rbind-2-vectors-of-different-lengths-by-their-names, and http://stackoverflow.com/questions/3365885/combining-vectors-of-unequal-length-into-a-data-frame?rq=1 are related – Thomas Aug 20 '13 at 12:53

1 Answers1

3

You could consider using ldply from plyr here.

set.seed(123)
#k is the length of each result
k <- sample( 5 , 3 , repl = TRUE )
#[1] 2 4 3


# Make a list of vectors, each a sequence from 1:k
ll <- lapply( k , function(x) seq_len(x) )
#[[1]]
#[1] 1 2

#[[2]]
#[1] 1 2 3 4

#[[3]]
#[1] 1 2 3

# take our list and rbind it into a data.frame, filling in missing values with NA
ldply( ll , rbind)
#  1 2  3  4
#1 1 2 NA NA
#2 1 2  3  4
#3 1 2  3 NA
Simon O'Hanlon
  • 58,647
  • 14
  • 142
  • 184
  • If he wants the vectors to be the `rows` of the dataframe, I think we don't need to take the transpose of the final result. This `ldply( ll , rbind)` simply should do it. – Mayou Aug 20 '13 at 12:53
  • 1
    By the way, very neat code. – Mayou Aug 20 '13 at 12:55
  • I see: using lists one avoids the curse of dimensionality :-) . Very helpful, thanks! – Avitus Aug 20 '13 at 13:41