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
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)
}
}
This print something like this:
L N
1 10 7
L N
1 10 7
L N
1 12 7
L N
1 12 7
But I need this print like:
L N
1 10 7
2 10 7
3 12 7
4 12 7
I try some function modification to this but dont work.
This is just an example, the real function is a little big.
Thanks
ROnaldo
--
Why, every one as they like; as the good woman said when she kissed her cow.
-- Rabelais
--
| // | \\ [*****************************][*******************]
|| ( ? ? ) [Ronaldo Reis J?nior ][PentiumIII-600 ]
| V [UFV/DBA-Entomologia ][HD: 30 + 10 Gb ]
|| / \ [36571-000 Vi?osa - MG ][RAM: 128 Mb ]
| /(.''`.)\ [Fone: 31-3899-2532 ][Video: SiS620-8Mb ]
||/(: :' :)\ [chrysopa at insecta.ufv.br ][Modem: Pctel-onboar]
|/ (`. `'` ) \[ICQ#: 5692561 ][Kernel: 2.4.18 ]
|| ( `- ) [*****************************][*******************]
||| _/ \_Powered by GNU/Debian W/Sarge D+ || Lxuser#: 205366
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