[R] Nested for loop help please.
arun
smartpink111 at yahoo.com
Sun Jan 12 21:26:01 CET 2014
Hi Mathew,
You must have noticed that I used:
output <-vector() # instead of output=array(NA,c(length(pick.a), length(pick.d)))
The below methods should give the results:
Method1:####
res <- t(sapply(pick.a,function(x) sapply(pick.d,function(y) {ts1 <- sum(t.sham(m.sham,x,y)); tc1 <- sum(t.control(m.control,x)); ts1/tc1})))
dim(res)
#[1] 200 100
Method2:### Creating a matrix of NAs
output <- array(NA,c(length(pick.a),length(pick.d)))
for(count in 1:length(pick.a)){
for(count1 in 1:length(pick.d)){
ts1= sum(t.sham(m.sham,pick.a[count],pick.d[count1]))
tc1=sum(t.control(m.control,pick.a[count]))
output[count,count1] <- ts1/tc1
}
}
dim(output)
#[1] 200 100
identical(res,output)
#[1] TRUE
###Method 3:
output1 <-vector() #instead of defining it as matrix
for(count in 1:length(pick.a)){
for(count1 in 1:length(pick.d)){
ts1 = sum(t.sham(m.sham,pick.a[count],pick.d[count1]))
tc1 <- sum(t.control(m.control,pick.a[count]))
output1= c(output1,ts1/tc1)
}
}
m1 <- matrix(output1,length(pick.a),length(pick.d),byrow=TRUE)
identical(m1,res)
#[1] TRUE
A.K.
On Sunday, January 12, 2014 12:49 PM, Mathew Nagendran <mathew.nagendran at mail.utoronto.ca> wrote:
Hi Arun, thank you for all your help. Considering I only started using R a few months ago, your help is very valuable to me.
I have taken the steps required to make my nested for loop work. The only problem I face now is trying to get my matrix to fill with outputs rather than NA. Is there something I can do to resolve this?
m.control=c(1.45,9.40,9.96,4.2,1.86,0.2)
m.sham=c(3.39,23.94,23.62,10.08,2.99,1.09)
t.control=function(m, a){(1-exp(-a*m))}
t.sham=function(m, a, d){(1-exp(-a*(1-d)*m))}
t.ratio=function(ts, tc){(ts/tc)}
pick.a=seq(0.01,2,by=0.01) #set of a values defined
pick.d=seq(0.01,1,by=0.01) #set of d values defined
output=array(NA,c(length(pick.a), length(pick.d))) #define array for Ts/Tc ratios. a values (0.01-2) in column 1 and d values (0.01-1) in the other columns.
for(count in 1:length(pick.a)){
for(count1 in 1:length(pick.d)){
ts1=sum(t.sham(m.sham,pick.a[count],pick.d[count1]))
tc1=sum(t.control(m.control,pick.a[count]))
output=c(output,ts1/tc1)
}
}
m1=matrix(output,200,100)
print(m1)
________________________________________
From: arun <smartpink111 at yahoo.com>
Sent: Saturday, January 11, 2014 6:00 PM
To: Mathew Nagendran
Subject: Re: [R] Nested for loop help please.
Hi,
I guess you need to replace t() with t.control() or t.sham()
output <-vector()
for(count in 1:length(pick.a)){
for(count1 in 1:length(pick.d)){
ts1 = sum(t.sham(m.sham,pick.a[count],pick.d[count1]))
tc1 <- sum(t.control(m.control,pick.a[count]))
output= c(output,ts1/tc1)
}
}
m1 <- matrix(output,200,100)
dim(m1)
#[1] 200 100
A.K.
On Saturday, January 11, 2014 3:20 PM, Mathew Nagendran <mathew.nagendran at mail.utoronto.ca> wrote:
Hi all I am relatively new to R. I am trying to create a nested for loop but i keep getting an error message (unused argument). Can someone help me find out where I am goign wrong?
> m.control=c(1.45,9.40,9.96,4.2,1.86,0.2)
> m.sham=c(3.39,23.94,23.62,10.08,2.99,1.09)
>
> t.control=function(m, a){(1-exp(-a*m))}
> t.sham=function(m, a, d){(1-exp(-a*(1-d)*m))}
>
> t.ratio=function(ts, tc){(ts/tc)}
>
> pick.a=seq(0.01,2,by=0.01) #set of a values defined
> pick.d=seq(0.01,1,by=0.01) #set of d values defined
>
> output=array(NA,c(length(pick.a), length(pick.d))) #define array for Ts/Tc ratios. a values (0.01-2) in column 1 and d values (0.01-1) in the other columns.
>
> for(count in 1:length(pick.a)){
+ for(count in 1:length(pick.d)){
+ ts=sum(t(m.sham,pick.a[count],pick.d[count]))
+ tc=sum(t(m.control,pick.a[count]))
+ output[count,2]= (ts/tc)
+ }
+ print(output)
+ }
Error in t(m.sham, pick.a[count], pick.d[count]) :
unused argument(s) (pick.a[count], pick.d[count])
[[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.
More information about the R-help
mailing list