[R] loop

John Kane jrkrideau at yahoo.ca
Wed Mar 5 19:30:28 CET 2008


m <- seq(-1,1,0.1)
  x1 <- vector()
  x2 <- vector()
# the loop statement was incorrect. 
  for(i in 1:length(m)){   
  x1[i] <- m[i]
  x2[i] <- m[i]^2
  }
  dat <- data.frame(x1,x2)

# But why not something like this? There is no need
for a loop.

x1 <- seq(-1,1,0.1)
mdat <- data.frame(x1, x2=x1^2)
--- Neuer Arkadasch <neuer_arkadasch at yahoo.de> wrote:

> Hello all,
>    
>   I am trying to use 
>    
>   m <- seq(-1,1,0.1)
>   x1 <- vector()
>   x2 <- vector()
>   for(i in m){
>   x1[i] <- i
>   x2[i] <- i^2
>   }
>   dat <- data.frame(x1,x2)
>   But, I have  false result
>   >dat
>     x1 x2
>   1 1  1
>    
>   could some tell me how it is possible to do this?
>    
>   Thank you!
>    
>    
> 
>        
> ---------------------------------
> 
> 	[[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.
>



More information about the R-help mailing list