R for loop

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

R for loop

matira
Hi
I'm quite new to R and I am having trouble with this code:

names(Mindreader_2012_vars)
varlist<-names(Mindreader_2012_vars)
for (i in 688:696) {
  for (j in 673:685) {
  assign(paste("Uk_Cluster_",i-687,sep=""),cbind(get(varlist[j],pos=Mindreader_2012_vars)[get(varlist[i], pos=Mindreader_2012_vars)>0]))
}
}

I want this code to output 9 matrixes (which are the variables 'i' is getting) which have 13 columns (one for each of the variables that 'j' is getting.
At the moment I get 9 matrixes with only 1 column. I guess the issue is the way I am nesting the loops but i can't work out how to make it take all the values of 'j' in each matrix.

Thanks
Reply | Threaded
Open this post in threaded view
|

Re: R for loop

David Carlson
I would be easier to respond if we had some idea what
Mindreader_2012_vars is, data.frame, list, matrix?

Give us a small, reproducible version of what you are trying to
accomplish.

-------------------------------------
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77840-4352

-----Original Message-----
From: [hidden email]
[mailto:[hidden email]] On Behalf Of matira
Sent: Friday, November 15, 2013 7:23 AM
To: [hidden email]
Subject: [R] R for loop

Hi
I'm quite new to R and I am having trouble with this code:

names(Mindreader_2012_vars)
varlist<-names(Mindreader_2012_vars)
for (i in 688:696) {
  for (j in 673:685) {
 
assign(paste("Uk_Cluster_",i-687,sep=""),cbind(get(varlist[j],po
s=Mindreader_2012_vars)[get(varlist[i],
pos=Mindreader_2012_vars)>0]))
}
}

I want this code to output 9 matrixes (which are the variables
'i' is
getting) which have 13 columns (one for each of the variables
that 'j' is
getting.
At the moment I get 9 matrixes with only 1 column. I guess the
issue is the
way I am nesting the loops but i can't work out how to make it
take all the
values of 'j' in each matrix.

Thanks



--
View this message in context:
http://r.789695.n4.nabble.com/R-for-loop-tp4680515.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
[hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: R for loop

Jeff Newmiller
You seem to have a problem with your Mindreader skills today, David!
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<[hidden email]>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
---------------------------------------------------------------------------
Sent from my phone. Please excuse my brevity.

David Carlson <[hidden email]> wrote:

>I would be easier to respond if we had some idea what
>Mindreader_2012_vars is, data.frame, list, matrix?
>
>Give us a small, reproducible version of what you are trying to
>accomplish.
>
>-------------------------------------
>David L Carlson
>Department of Anthropology
>Texas A&M University
>College Station, TX 77840-4352
>
>-----Original Message-----
>From: [hidden email]
>[mailto:[hidden email]] On Behalf Of matira
>Sent: Friday, November 15, 2013 7:23 AM
>To: [hidden email]
>Subject: [R] R for loop
>
>Hi
>I'm quite new to R and I am having trouble with this code:
>
>names(Mindreader_2012_vars)
>varlist<-names(Mindreader_2012_vars)
>for (i in 688:696) {
>  for (j in 673:685) {
>
>assign(paste("Uk_Cluster_",i-687,sep=""),cbind(get(varlist[j],po
>s=Mindreader_2012_vars)[get(varlist[i],
>pos=Mindreader_2012_vars)>0]))
>}
>}
>
>I want this code to output 9 matrixes (which are the variables
>'i' is
>getting) which have 13 columns (one for each of the variables
>that 'j' is
>getting.
>At the moment I get 9 matrixes with only 1 column. I guess the
>issue is the
>way I am nesting the loops but i can't work out how to make it
>take all the
>values of 'j' in each matrix.
>
>Thanks
>
>
>
>--
>View this message in context:
>http://r.789695.n4.nabble.com/R-for-loop-tp4680515.html
>Sent from the R help mailing list archive at Nabble.com.
>
>______________________________________________
>[hidden email] 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.
>
>______________________________________________
>[hidden email] 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.

______________________________________________
[hidden email] 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.