An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20121225/b9920733/attachment.pl>
for loop not working
9 messages · Pascal Oettli, arun, Eliza Botto
Hello, ?seq seq(i,444,37) with i = 1,2,3...37 HTH Pascal Le 25/12/2012 14:03, eliza botto a ?crit :
dear R family,i have a matrix of 444 columns. what i want to do is the following. 1. starting from column 1 i want to select every 37th column on the way. more precisely i want to select column 1, 38,75,112,149 and so on. 2.starting from column 2, i again want to select every 37th column. which means 2,39,76,113,150 and so on. similarly starting from 3 till 37th column. i have tried following loop command which is not working.can anyone plz see whats wrong in that?
for (i in 1:37)
{
a<-e[,e[i]:444]
}
lapply(seq_len(1),
function(i) {
a[,c(TRUE, rep(FALSE,1))]
})
extremly sorry for bothering you once again.. eliza [[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.
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20121225/f5ee1a59/attachment.pl>
Hello, I provided you all the information you need. Replace i by 1,2,3...37 in seq(i,444,37). Pascal Le 25/12/2012 14:19, eliza botto a ?crit :
Dear Pascal, thanks for replying. it is working about it only executes the first set of column, it does not bring about all the sets of columns. any thoughts?? eliza
> Date: Tue, 25 Dec 2012 14:12:01 +0900 > From: kridox at ymail.com > To: eliza_botto at hotmail.com > CC: r-help at r-project.org > Subject: Re: [R] for loop not working > > Hello, > > ?seq > > seq(i,444,37) > > with i = 1,2,3...37 > > HTH > Pascal > > > Le 25/12/2012 14:03, eliza botto a ?crit :
> > > > dear R family,i have a matrix of 444 columns. what i want to do is
the following.
> > 1. starting from column 1 i want to select every 37th column on the
way. more precisely i want to select column 1, 38,75,112,149 and so on.
> > 2.starting from column 2, i again want to select every 37th column.
which means 2,39,76,113,150 and so on.
> > similarly starting from 3 till 37th column. > > i have tried following loop command which is not working.can anyone
plz see whats wrong in that?
> >> for (i in 1:37)
> >
> >> {
> > > >
> >> a<-e[,e[i]:444]
> > > >
> >> }
> > > >
> >> lapply(seq_len(1),
> > function(i) {
> >
> >
> >> a[,c(TRUE, rep(FALSE,1))]
> > > >
> >> })
> > extremly sorry for bothering you once again.. > > eliza > > [[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. > >
HI Eliza,
You could try this:
set.seed(15)
mat1<-matrix(sample(1:2000,1776,replace=TRUE),ncol=444)
colnames(mat1)<-paste("Col",1:444,sep="")
res<-lapply(seq_len(ncol(mat1)),function(i) mat1[,seq(i,444,37)])
#If you want only this from 1:37, then
?res1<-lapply(1:37,function(i) mat1[,seq(i,444,37)])
A.K.
----- Original Message -----
From: eliza botto <eliza_botto at hotmail.com>
To: "r-help at r-project.org" <r-help at r-project.org>
Cc:
Sent: Tuesday, December 25, 2012 12:03 AM
Subject: [R] for loop not working
dear R family,i have a matrix of 444 columns. what i want to do is the following.
1. starting from column 1 i want to select every 37th column on the way. more precisely i want to select column 1, 38,75,112,149 and so on.
2.starting from column 2, i again want to select every 37th column. which means 2,39,76,113,150 and so on.
similarly starting from 3 till 37th column.
i have tried following loop command which is not working.can anyone plz see whats wrong in that?
for (i in 1:37)
{
a<-e[,e[i]:444]
}
lapply(seq_len(1),
function(i) {
a[,c(TRUE, rep(FALSE,1))]
})
extremly sorry for bothering you once again.. eliza ??? ??? ??? ? ??? ??? ? ??? [[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.
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20121225/e2a40423/attachment.pl>
Hi, Why don't you use Arun's solution? Regards, Pascal Le 25/12/2012 15:57, eliza botto a ?crit :
Dear Arun,
as usuall you were spot on. i tried the following
*_>lapply(_**_seq_len(ncol(e)), function(i) {_*
_
_
_>a<-e[,(e[i]:444)]_
>a[,c(TRUE, rep(FALSE,36))]
})
but it never worked. thanks for your kind help. lots of love elisa
> Date: Mon, 24 Dec 2012 22:40:08 -0800
> From: smartpink111 at yahoo.com
> Subject: Re: [R] for loop not working
> To: eliza_botto at hotmail.com
> CC: r-help at r-project.org; kridox at ymail.com
>
> HI Eliza,
>
> You could try this:
> set.seed(15)
> mat1<-matrix(sample(1:2000,1776,replace=TRUE),ncol=444)
> colnames(mat1)<-paste("Col",1:444,sep="")
> res<-lapply(seq_len(ncol(mat1)),function(i) mat1[,seq(i,444,37)])
>
> #If you want only this from 1:37, then
> res1<-lapply(1:37,function(i) mat1[,seq(i,444,37)])
>
>
> A.K.
>
>
>
> ----- Original Message -----
> From: eliza botto <eliza_botto at hotmail.com>
> To: "r-help at r-project.org" <r-help at r-project.org>
> Cc:
> Sent: Tuesday, December 25, 2012 12:03 AM
> Subject: [R] for loop not working
>
>
> dear R family,i have a matrix of 444 columns. what i want to do is
the following.
> 1. starting from column 1 i want to select every 37th column on the
way. more precisely i want to select column 1, 38,75,112,149 and so on.
> 2.starting from column 2, i again want to select every 37th column.
which means 2,39,76,113,150 and so on.
> similarly starting from 3 till 37th column. > i have tried following loop command which is not working.can anyone
plz see whats wrong in that?
> >for (i in 1:37)
>
> >{
> >
> >a<-e[,e[i]:444]
> >
> >}
> >
> > lapply(seq_len(1),
> function(i) {
>
>
> >a[,c(TRUE, rep(FALSE,1))]
> >
> >})
> extremly sorry for bothering you once again.. > eliza > [[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. >
HI Eliza,
Try this:
set.seed(15)
mat1<-matrix(sample(1:2000,1776,replace=TRUE),ncol=444)
colnames(mat1)<-paste("Col",1:444,sep="")
res1<-lapply(1:37,function(i) mat1[,seq(i,444,37)])
res2<-lapply(1:37,function(i) {a<-mat1[,i:444];a[,c(TRUE,rep(FALSE,36))]}) #your code
identical(res1,res2)
#[1] TRUE
From: eliza botto <eliza_botto at hotmail.com>
To: "smartpink111 at yahoo.com" <smartpink111 at yahoo.com>
Cc: "r-help at r-project.org" <r-help at r-project.org>; kridox at ymail.com
Sent: Tuesday, December 25, 2012 1:57 AM
Subject: RE: [R] for loop not working
To: "smartpink111 at yahoo.com" <smartpink111 at yahoo.com>
Cc: "r-help at r-project.org" <r-help at r-project.org>; kridox at ymail.com
Sent: Tuesday, December 25, 2012 1:57 AM
Subject: RE: [R] for loop not working
Dear Arun,
as usuall you were spot on. i tried the following
>lapply(seq_len(ncol(e)), function(i) {
>a<-e[,(e[i]:444)]
>a[,c(TRUE, rep(FALSE,36))]
>})?
but it never worked.?
thanks for your kind help.
lots of love
elisa
> Date: Mon, 24 Dec 2012 22:40:08 -0800
> From: smartpink111 at yahoo.com
> Subject: Re: [R] for loop not working
> To: eliza_botto at hotmail.com
> CC: r-help at r-project.org; kridox at ymail.com
>
> HI Eliza,
>
> You could try this:
> set.seed(15)
> mat1<-matrix(sample(1:2000,1776,replace=TRUE),ncol=444)
> colnames(mat1)<-paste("Col",1:444,sep="")
> res<-lapply(seq_len(ncol(mat1)),function(i) mat1[,seq(i,444,37)])
>
> #If you want only this from 1:37, then
> ?res1<-lapply(1:37,function(i) mat1[,seq(i,444,37)])
>
>
> A.K.
>
>
>
> ----- Original Message -----
> From: eliza botto <eliza_botto at hotmail.com>
> To: "r-help at r-project.org" <r-help at r-project.org>
> Cc:
> Sent: Tuesday, December 25, 2012 12:03 AM
> Subject: [R] for loop not working
>
>
> dear R family,i have a matrix of 444 columns. what i want to do is the following.
> 1. starting from column 1 i want to select every 37th column on the way. more precisely i want to select column 1, 38,75,112,149 and so on.
> 2.starting from column 2, i again want to select every 37th column. which means 2,39,76,113,150 and so on.
> similarly starting from 3 till 37th column.
> i have tried following loop command which is not working.can anyone plz see whats wrong in that?
> >for (i in 1:37)
>
> >{
>
>
> >a<-e[,e[i]:444]
>
>
> >}
>
>
> > lapply(seq_len(1),
> function(i) {
>
>
> >a[,c(TRUE, rep(FALSE,1))]
>
>
> >})
> extremly sorry for bothering you once again..
> eliza ??? ???????? ?????? ??? ?
> ??? [[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.
>
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20121225/a6121709/attachment.pl>