[R] programing problem with for( )
Henrique Dallazuanna
wwwhsd at gmail.com
Mon Feb 1 12:38:07 CET 2010
Try this:
s <-matrix(data=NA,nrow=1,ncol=5 )
for( i in 1:5) {
k <- sqrt(i)
s[,i] <- k
print(k)
}
Or:
s[] <- sqrt(1:5)
On Mon, Feb 1, 2010 at 9:29 AM, marlene marchena
<marchenamarlene at gmail.com> wrote:
> Hi R-users
>
> I'm writing a code to run a fuction but I found an error that I can't fix. I
> reproduced the error with a simple example.
>
> The correct answer is k but I can't fill my s matrix. What I'm doing wrong?
>
>
> s<-matrix(data=NA,nrow=1,ncol=5 )
> s
>
> for(i in 1:5)
> {
> k=sqrt(i)
> s[,i]<-k[i]
> print(k)
> }
>
> s
>
> Thanks in advance,
>
> Marlene.
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
--
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O
More information about the R-help
mailing list