[R] Is there a faster way to do this?

2010-03-25 Thread Márcio Resende

Hi guys, I am still learning R, and not well familiar with all the apply
functions.
I am trying to find faster alternatives to replace the for cycle.
Is there a faster way to do the example below?

nm - 1000
b - matrix (rnorm (5000, 0, 1), nrow = 500, ncol = nm)
a - matrix (0, nm, nm)
for (i in 1 : nm) {
for (j in 1 : nm) {
if ( j == i) {
next }
a[i, j] - t (b [, i]) %*% b[, j]
}
}

thanks

-- 
View this message in context: 
http://n4.nabble.com/Is-there-a-faster-way-to-do-this-tp1691601p1691601.html
Sent from the R help mailing list archive at Nabble.com.

__
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] Dimnames of array inside loop

2010-03-18 Thread Márcio Resende

I read the post
http://n4.nabble.com/Error-setting-rowname-if-rowname-currently-NULL-td794579.html#a794579
where it says that the problem is that 
dimnames(acuracia) is Null -equivalent to rownames(fred) is NULL (in the
post)
Then I tried to define all the dimnames first as random names to be then
replaced in the loop
but it did not work either?

Does anyone have a clue?
Thanks a lot

-- 
View this message in context: 
http://n4.nabble.com/Dimnames-of-array-inside-loop-tp1594215p1598069.html
Sent from the R help mailing list archive at Nabble.com.

__
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] storing matrix(variables) in loop

2010-03-15 Thread Márcio Resende

Hello R-helpers,
I have the following code that works well, 

b -list()
for (i in 1:3){
a - matrix(runif(1),3,3)
b[[i]] - a
}
b

however, I need to do something similar with two loops and I was looking for
something that would look like

b - list()
for (i in 1:3){
for (j in 1:4){
a - matrix(runif(1),3,3)
b[[i,j]] - a #but this doesn´t work
}
}

Anyway, I wanted b to loop like
[[i=1, j=1]] [[i=1, j=2]] (...)
a[i=1, j=1]   a[i=1,j=2]  (...)

[[i = 2, j=1]](...)
a[i = 2, j = 1]   (...)

(...)

Can anybody help me?
Thanks

-- 
View this message in context: 
http://n4.nabble.com/storing-matrix-variables-in-loop-tp1593461p1593461.html
Sent from the R help mailing list archive at Nabble.com.

__
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] Dimnames of array inside loop

2010-03-15 Thread Márcio Resende

hello R-helpers,

I have an array

acuracia - array(NA, dim = c(1, 1, A, B, C))

which is first defined and in the example above with dimensions 1x1xAxBxC.
My array is then filled using 3 loops (I am not well familiar yet with
lapply or sapply functions so I am still a loop-user):

for (i in 1:A){
for (j in 1:B){
for (k in 1:C){
acuracia[,,i,j,k] - correlacao / (sqrt(ac))
}
}
}


then I want to put names using dimnames but filling the names each at a time
after each iteration:

My first idea was to try

acuracia - array(NA, dim = c(1, 1, A,B,C),dimnames = list(acc,NULL,
paste(rep., i, sep =  ), paste(variable: , j), paste(mark.val = ,
k))

but it did not work.
Can anybody help?
Thank you very much

-- 
View this message in context: 
http://n4.nabble.com/Dimnames-of-array-inside-loop-tp1594215p1594215.html
Sent from the R help mailing list archive at Nabble.com.

__
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] how to track a number in a row

2010-01-11 Thread Márcio Resende

Hi,
I have a 100x15 matrix and in each row a set of 15 random numbers out of 25.
for example:

b - c(1:25)
a -matrix(0,10,15)
for (i in 1:10){
a[i,] - sample(b,15,replace = FALSE)
}

I would like to create another matrix (25x100), for example d with the
probability of each number from the first matrix
Therefore I need to track, for example, if number 1 is present in the first
row (d[1,1]) (which would give me an probability of 1 out of 1).
Then, track again if number 1 is present on the second row (d[2,1]) (And if
not, the probability would be 1 out of 2 = 0.5)...and so on for all the 25
collumns (25 numbers) and all the 100 rows.

Could anybody help how to do it??
Thanks in advance
Marcio


-- 
View this message in context: 
http://n4.nabble.com/how-to-track-a-number-in-a-row-tp1011787p1011787.html
Sent from the R help mailing list archive at Nabble.com.

__
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] how to track a number in a row

2010-01-11 Thread Márcio Resende

Therefore I need to track, for example, if number 1 is present in the first
row (d[1,1]) (which would give me an probability of 1 out of 1).

Just to make it clear, I need to track on matrix a if number 1 is present
in the first row to fill the spot d[1,1] where the first collumn is related
to number 1 and the first row, related to row 1 on matrix a

Thanks again




Márcio Resende wrote:
 
 Hi,
 I have a 100x15 matrix and in each row a set of 15 random numbers out of
 25.
 for example:
 
 b - c(1:25)
 a -matrix(0,10,15)
 for (i in 1:10){
 a[i,] - sample(b,15,replace = FALSE)
 }
 
 I would like to create another matrix (25x100), for example d with the
 probability of each number from the first matrix
 Therefore I need to track, for example, if number 1 is present in the
 first row (d[1,1]) (which would give me an probability of 1 out of 1).
 Then, track again if number 1 is present on the second row (d[2,1]) (And
 if not, the probability would be 1 out of 2 = 0.5)...and so on for all the
 25 collumns (25 numbers) and all the 100 rows.
 
 Could anybody help how to do it??
 Thanks in advance
 Marcio
 
 
 

-- 
View this message in context: 
http://n4.nabble.com/how-to-track-a-number-in-a-row-tp1011787p1011788.html
Sent from the R help mailing list archive at Nabble.com.

__
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] Assign variables in a loop to a list

2009-12-09 Thread Márcio Resende

Dear R helpers,
I am new in R and I am having trouble with a function.
I am proggraming a genetic analysis and there is a script that generates a
lot of different matrix, for example x,y and z.
And what I am trying to do is to loop the same script for different
variables the user will define in the function. Therefore the matrix x, y
and z are going to be created for each variable

myfun - function(...){#the variables in (...) for example (DAP,
Vol)
Vec = matrix(c(...))
for (i in seq(along = Vec)){
... ##generates x, y and z
# The scripts are not here because they are big

assign(paste(x,i,sep=),x)
assign(paste(y,i,sep=),y)  #this generates x1,y1,z1, x2, y2, and z2 for
the example with two variables
assign(paste(z,i,sep=),z)
}

Here is the step of my doubt where I can´t solve
 I want to assign those variables in a list function

structure(list(...), class = genotype) ## In the example it would be

##structure(list(varX1 = x1, varX2 = x2, varY1 = y1, varY2 = y2, varZ1 = z1,
varZ2 = z2), class = genotype)

} #end of function

However I don´t know how to assign those variables in this list because I
don´t know how many variables will the user declare

I am not sure if I was clear, I know it is hard without the whole script,
but I think it wouldn´t make any difference. It could be considered 3
randomly matrix generated each time (each loop).

Thank you very much for the help and for the time dispended



-- 
View this message in context: 
http://n4.nabble.com/Assign-variables-in-a-loop-to-a-list-tp956207p956207.html
Sent from the R help mailing list archive at Nabble.com.

__
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] equivalent of ifelse

2009-12-09 Thread Márcio Resende



David Winsemius wrote:
 
 
 On Dec 9, 2009, at 12:40 PM, carol white wrote:
 
 Hi,
 Is there any equivalent for ifelse (except if (cond) expr1 else  
 expr2) which takes an atomic element as argument but returns vector  
 since ifelse returns an object of the same length as its argument?

 x = c(1,2,3)
 y = c(4,5,6,7)
 z = 3

 ifelse(z = 3,x,y)

 would return x and not 1
 
 I worry that this is too simple, so wonder if you have expressed your  
 intent clearly.
 
   if(z = 3) {x} else {y}
 [1] 1 2 3

 
 I was wondering David, why is the {} necessary?
 if(z = 3) x else y 
 [1] 1 2 3
 
 since without {} it cames with the same result?
 
 Thanks
 MR.
 
 
 
 David Winsemius, MD
 Heritage Laboratories
 West Hartford, CT
 
 __
 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.
 
 

-- 
View this message in context: 
http://n4.nabble.com/equivalent-of-ifelse-tp956232p956258.html
Sent from the R help mailing list archive at Nabble.com.

__
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] Using two (...) in a function

2009-12-01 Thread Márcio Resende

Hello R-Helpers,
I am not sure if it is a very simple question but I would like to use two
(...) in a function, for example,

this is a script where I would like to input the variable names (in one of
the (...)) and the variances associated to those variables which are not
calculated in the script because there is a specific software to calculate
it (the other (...))

data - function (DAP, ALT, var1, var2){   My wish was to do
somethin like function (...,...){
Vec - matrix(c(DAP, ALT))###here it would came the first (...) Vec
- matrix(c(...))
for (i in seq(along - Vec)){
caracteristica - Vec[i]

varF - if (caracteristica == DAP) var1 else var2##here I would like
to do something like #if   ###caracteristica == variable1) variance1 else if
(caracteristica == variable2) variance2 else ...
}
}

but to turn this in a function, I would like to replace (DAP, ALT) by
any variable and var1, var2 to any variance, and not only necessary 2, for
example
data (variable1,variable2,variable3, variance1, variance2,
variance3)

I am not sure if I made myself clear and if this is an answerable doubt,
anyway thank you vey much
Márcio
-- 
View this message in context: 
http://n4.nabble.com/Using-two-in-a-function-tp932200p932200.html
Sent from the R help mailing list archive at Nabble.com.

__
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.