[R] Dataframe in loop
Michael Na Li
lina at u.washington.edu
Wed Mar 5 20:04:32 CET 2003
On Wed, 5 Mar 2003, Ronaldo Reis, Jr. verbalised:
> Hi,
>
> I try to make a dataframe in a loop function, but I dont have succeed.
>
> The function is something like this:
>
> for(i in c(10,12)) {
> expr
^^^^ what's this?
> for(j in c(1:2) {
> total <- c(1,2,3,4,5,6,7)
> nspf <- length(levels(as.factor(total)))
> fin <- data.frame(L=i,N=nspf)
> print(fin)
> }
> }
>
You are creating a new data frame with in each loop.
You want something like this,
,----
| > fin <- NULL
| > for(i in c(10,12)) {
| + for(j in c(1:2)) {
| + total <- c(1,2,3,4,5,6,7)
| + nspf <- length(levels(as.factor(total)))
| + fin <- rbind (fin, c(L=i, N=nspf))
| + }
| + }
| > print(fin)
| L N
| [1,] 10 7
| [2,] 10 7
| [3,] 12 7
| [4,] 12 7
`----
Cheers,
Michael
More information about the R-help
mailing list