[R] controlling the number of times a script is repeated in a loop

2008-11-30 Thread Salas, Andria Kay
I am running a large for loop and at the end of each iteration a matrix is 
produced.  The program changes the columns in the matrix, and each time a 
column is added the name of that column is y.  All original columns have no 
column name.  Due to the nature of the program, all original columns will 
eventually be replaced with new columns each with the column name y. It is at 
this point that I want the program to stop running the for loop (right now I 
just have it running for 20 iterations resulting in a matrix with a mix of old 
and new columns).  I was thinking maybe I could do something with an if else 
statement and the command colnames.  For example:

If(colnames all are y) (stop) else (run the program again)

Not entirely sure how to go about this though, or if I am even on the right 
track.  Maybe I need to get rid of the master for loop as perhaps for loops are 
more constraining in the number of times the program is run?  The number of 
runs to get all original columns replaced with new ones will never be the same 
(I plan on running this several times, with time steps ending when all original 
columns are replaced.  Perhaps at the end of each time step I can set column 
names to have no column names (so from ys to nothing), allowing me to use 
this method for each time step)...

I know it is always helpful to have some code to work with, but it seems 
impractical to send the whole program as one part if it I am still working on 
and would probably just create more confusion.  Hopefully someone might be able 
to offer a few suggestions...I can try to come up with a toy program to send 
along but I am not good enough in R yet to do this quickly!

Thank you for all the help and I am sorry if this question is perhaps a bit 
confusing!  If I have not made my problem and what I hope to do clear, please 
let me know.

__
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] if then statement problem

2008-11-27 Thread Salas, Andria Kay
I really need help with an if then statement I am having trouble with.  A brief 
explanation of what I am trying to do: I have a p matrix of all permutations of 
a vector of length 3 filled with either 1s or -1s (these make up the columns).  
The p.unique matrix is the unique columns of the p matrix; here, they are 
identical but in my real script this will not always be the case.  I want to be 
able to take a randomly generated vector and see which column in the p.unique 
matrix, if any, match the random vector.  If there is a match, I get an integer 
returned that tells me which column is the match.  If there is no match, I get 
the response integer (0).  I want to write an if then statement that allows me 
to do different things based upon if there is a match to one of the columns in 
the p.unique matrix or not.  Below is a sample script plucked from bits of my 
master script that show this problem.  The vector r was made to not match 
any columns in p.unique.  If it did, c wou!
 ld equal the integer representing the matching column, and since it does not, 
I want c to equal the vector (0,0,0).  My if then statement is not working (I 
keep getting the error that my argument is of length zero), so I would really 
appreciate any help anyone can provide me.  Thanks for all of your help 
previously and in advance for the help with this problem!!  Happy Thanksgiving!

p=as.matrix(do.call(expand.grid,rep(list(c(-1,1)),3)))  
p=t(p)
p.unique=unique(p,MARGIN=2)   

r=c(2,2,2)
q=which(apply(p.unique,2,function(x)all(x==r)))
if(q0){c=p.unique[,q]};{c=c(0,0,0)} 

__
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] matching matrix columns to a vector

2008-11-24 Thread Salas, Andria Kay
I need help with (hopefully) just one more thing.  I have been fussing with 
this for quite some time and have decided just to give up and ask!  I want to 
match a column in a matrix to a vector.  I found a which command that I 
thought would be helpful as it does the following:

 g=c(1,5,3,2,7)
 which(g==5)
[1] 2

As the above gave which placement in the g vector corresponded to 5 (the second 
place), I need this command to give me which column in a matrix matches to a 
vector.

This is just a toy example of what I am trying to do:
 t=matrix(1:12,3,4)
v=c(1,2,3)
which(t[,j]==v)

This does not work, and with my real matrices and vectors, I was getting 
outputs that did not make sense.  These examples are more to give an idea of 
what I am aiming to accomplish.

Thank you for all the help!!
__
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] unique columns in a matrix and number of each

2008-11-22 Thread Salas, Andria Kay
I need help determining the unique columns of a matrix and the numbers of each 
unique column.  For example, let's say I have a matrix with 6 columns, 2 of 
these are filled with the value of 1 and the other 4 are filled with the value 
of 0.  I would then like to have a command that tells me what the unique 
columns are (so columns with 1s and columns with 0s) and the number of each 
type of column (so 2 columns of 1s and 4 columns of 0s) that occur in the 
matrix.  

Thank you for any help anyone can provide!!  I have been very impressed with 
the help that I have received so far!!  Thank you!  
__
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] (no subject)

2008-11-18 Thread Salas, Andria Kay
I need help with another problem I am having that deals with the generation of 
vectors that I asked about yesterday.  I now need to have each value in the 
vector (all values either 1 or -1) have a probability p that it will switch 
signs (so, say, each value has a 50% chance of switching from -1 to 1 or visa 
versa).  What would be the best way to go about doing this?  Thank-you!!
__
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] random value changes in a vector

2008-11-18 Thread Salas, Andria Kay
To clear up a question regarding my earlier posting regarding random changes in 
a vector:

Say you have a vector (1, -1, -1, 1, 1, -1).  I want each value in the vector 
to have a probability that it will change signs when this vector is 
regenerated.  For example, probability = 50%.  When the vector is regenerated, 
the first value in the vector (1) has a 50% chance of switching to -1.  If I 
regenerated this vector 10 times, 5 of the times it would switch to -1.  
Similarly, I need each value in the vector to have this same probability of 
switching signs when the vector is regenerated, and each value's chances of 
doing so is independent of the other values.  So the second value (-1) also has 
a 50% chance of switching to 1, and whether or not it does so is independent of 
if the first value changes from 1 to -1 (also a 50% probability). 

 I hope this clears up the confusion, and I would appreciate any help anyone 
can provide!
__
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] (no subject)

2008-11-17 Thread Salas, Andria Kay
I need help generating all possible combination of a vector of randomly 
assigned values of 1 and -1.  For example, a vector of 20 randomly placed 1s 
and -1s, and all possibilities (which would amount to 2^20 total vectors).  I 
am able to generate one such vector (via sample(c(-1,1),20,replace=TRUE)), but 
I do not know how to generate all the possibilities, which would need to be 
placed in a matrix.  Any help would be greatly appreciated!!! 
__
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.