Skip to content

loop

8 messages · Neuer Arkadasch, Giovanni Petris, Ravi Varadhan +4 more

#
It does not seem to be false: I am getting the same result. You should
probably explain what you expected and what you are trying to achieve.

Giovanni

  
    
#
Try this:

m <- seq(-1,1,0.1)
  x1 <- vector(length=length(m))
  x2 <- vector(length=length(m))
  for(i in m){
  x1[i] <- i
  x2[i] <- i^2
  }
  dat <- data.frame(x1,x2)

Ravi.

----------------------------------------------------------------------------
-------

Ravi Varadhan, Ph.D.

Assistant Professor, The Center on Aging and Health

Division of Geriatric Medicine and Gerontology 

Johns Hopkins University

Ph: (410) 502-2619

Fax: (410) 614-9625

Email: rvaradhan at jhmi.edu

Webpage:  http://www.jhsph.edu/agingandhealth/People/Faculty/Varadhan.html

 

----------------------------------------------------------------------------
--------


-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of Neuer Arkadasch
Sent: Wednesday, March 05, 2008 10:00 AM
To: r-help at stat.math.ethz.ch
Subject: [R] loop

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!
   
   

       
---------------------------------


______________________________________________
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.
#
Why not simply?

m <- seq(-1, 1, by = 0.1)
dat <- data.frame(m, m^2)

- Erik Iverson
Neuer Arkadasch wrote:
#
m <- seq(-1,1,0.1)
  x1 <- c()
  x2 <- c()
  for(i in 1:length(m)){
  x1[i] <- m[i]
  x2[i] <- m[i]^2
  }
  dat <- data.frame(x1,x2)

-----Original Message-----
From: r-help-bounces at r-project.org
[mailto:r-help-bounces at r-project.org]On Behalf Of Neuer Arkadasch
Sent: March 5, 2008 10:00 AM
To: r-help at stat.math.ethz.ch
Subject: [R] loop


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!




---------------------------------


______________________________________________
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.
#
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:

            
#
Hi

r-help-bounces at r-project.org napsal dne 05.03.2008 18:04:35:
The same result you will get by

dat2<-data.frame(x1=m, x2=m^2)
[1] TRUE

Regards
Petr
http://www.R-project.org/posting-guide.html
http://www.R-project.org/posting-guide.html