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 k
th 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.