Re: [R] Loop on vector name
Thanks for this mail. It runs perfectly, but now I stuck on how to convert the result in a vector format for further matrix-compputation. e.g. How to convert sapply(lis, sd) to vector? --- On Wed, 9/17/08, Dimitris Rizopoulos [EMAIL PROTECTED] wrote: From: Dimitris Rizopoulos [EMAIL PROTECTED] Subject: Re: [R] Loop on vector name To: [EMAIL PROTECTED] Cc: [EMAIL PROTECTED] Date: Wednesday, September 17, 2008, 5:32 PM you need to get(), e.g., try this: dat1 - rnorm(5) dat2 - rnorm(6) dat3 - rnorm(7) lis - lapply(paste(dat, 1:3, sep = ), get) lis sapply(lis, sd) I hope it helps. Best, Dimitris Megh Dal wrote: [My previous message rejected, therefore I am sending same one with some modification] I have 3 vectors with object name : dat1, dat2, dat3 Now I want to create a loop, like : for (i in 1:3) { cat(sd(dati)) } How I can do this in R? Regards, __ R-help@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. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 __ R-help@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.
Re: [R] Loop on vector name
oh god!! I used lapply() instead sapply(). Now it is ok. Thank you so much. By the way after a long time I saw your post. Hope you are well and everything going on fine :) --- On Fri, 9/19/08, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: From: [EMAIL PROTECTED] [EMAIL PROTECTED] Subject: Re: [R] Loop on vector name To: [EMAIL PROTECTED] Date: Friday, September 19, 2008, 11:20 AM hi: i'm not sure if i'm understanding your question because the sapply output is a vector. see below. dat1 - rnorm(5) dat2 - rnorm(6) dat3 - rnorm(7) lis - lapply(paste(dat, 1:3, sep = ), get) lis output - sapply(lis, sd) print(output) print(str(output)) On Fri, Sep 19, 2008 at 1:44 AM, Megh Dal wrote: Thanks for this mail. It runs perfectly, but now I stuck on how to convert the result in a vector format for further matrix-compputation. e.g. How to convert sapply(lis, sd) to vector? --- On Wed, 9/17/08, Dimitris Rizopoulos [EMAIL PROTECTED] wrote: From: Dimitris Rizopoulos [EMAIL PROTECTED] Subject: Re: [R] Loop on vector name To: [EMAIL PROTECTED] Cc: [EMAIL PROTECTED] Date: Wednesday, September 17, 2008, 5:32 PM you need to get(), e.g., try this: dat1 - rnorm(5) dat2 - rnorm(6) dat3 - rnorm(7) lis - lapply(paste(dat, 1:3, sep = ), get) lis sapply(lis, sd) I hope it helps. Best, Dimitris Megh Dal wrote: [My previous message rejected, therefore I am sending same one with some modification] I have 3 vectors with object name : dat1, dat2, dat3 Now I want to create a loop, like : for (i in 1:3) { cat(sd(dati)) } How I can do this in R? Regards, __ R-help@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. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 __ R-help@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. __ R-help@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.
Re: [R] Loop on vector name
Try this: sapply(ls(patt=^dat[0-9]), function(x)sd(get(x))) On Wed, Sep 17, 2008 at 8:50 AM, Megh Dal [EMAIL PROTECTED] wrote: [My previous message rejected, therefore I am sending same one with some modification] I have 3 vectors with object name : dat1, dat2, dat3 Now I want to create a loop, like : for (i in 1:3) { cat(sd(dati)) } How I can do this in R? Regards, __ R-help@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 [[alternative HTML version deleted]] __ R-help@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.
Re: [R] Loop on vector name
you need to get(), e.g., try this: dat1 - rnorm(5) dat2 - rnorm(6) dat3 - rnorm(7) lis - lapply(paste(dat, 1:3, sep = ), get) lis sapply(lis, sd) I hope it helps. Best, Dimitris Megh Dal wrote: [My previous message rejected, therefore I am sending same one with some modification] I have 3 vectors with object name : dat1, dat2, dat3 Now I want to create a loop, like : for (i in 1:3) { cat(sd(dati)) } How I can do this in R? Regards, __ R-help@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. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 __ R-help@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.
Re: [R] Loop on vector name
Others have answered the question that you asked (it is also a variation of Faq 7.21), but here is an answer to the question that you should have asked: When working with datasets like this, it is better to create a list rather than separate objects with names like dat1, dat2, etc. For example: mydat - list(dat1=rnorm(10), dat2=rnorm(25), dat3=runif(100)) Now your loop can be: for(i in 1:3) cat( sd( mydat[[i]] ) ) Or even simpler: sapply( mydat, sd ) And when you are through with the data, you only need to delete one object, or copy one object, or save one object, etc. Hope this helps, -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] project.org] On Behalf Of Megh Dal Sent: Wednesday, September 17, 2008 5:51 AM To: [EMAIL PROTECTED] Subject: [R] Loop on vector name [My previous message rejected, therefore I am sending same one with some modification] I have 3 vectors with object name : dat1, dat2, dat3 Now I want to create a loop, like : for (i in 1:3) { cat(sd(dati)) } How I can do this in R? Regards, __ R-help@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. __ R-help@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.