[R] calculating ratios from all combinations

R. Michael Weylandt michael.weylandt at gmail.com
Mon Oct 17 22:31:53 CEST 2011


On Mon, Oct 17, 2011 at 4:31 PM, R. Michael Weylandt
<michael.weylandt at gmail.com> wrote:
> I have no idea what's going on in the code you provided, particularly
> why there are no ratios given your subject line or why you are looping
> over an unused variable, but how about this:
>
> n <- colnames(d)
> n <- n[-length(n)] # Throwout unwanted columns
> n <- expand.grid(n,n)
>
> res <- apply(n, 1, function(x) d[,x[1]]/d[,x[2]])
> colnames(res) <- apply(n,1,paste,collapse="/")
>
> Michael Weylandt
>
> On Mon, Oct 17, 2011 at 1:39 PM, 1Rnwb <sbpurohit at gmail.com> wrote:
>> so here is the code I need help to fix the line for 'st' when it reaches
>> maximum, that's where i am getting stuck.
>>
>> thanks
>> Sharad
>> set.seed(100)
>>  d=data.frame(x=rnorm(20)+5,
>>  x1=rnorm(20)+5,
>>  x2=rnorm(20)+5,
>>  x3=rnorm(20)+5,
>>  x4=rnorm(20)+5,
>>  x5=rnorm(20)+5,
>>  x6=rnorm(20)+5,
>>  x7=rnorm(20)+5,
>>  x8=rnorm(20)+5)
>>  r2=c()
>>  cinit=0
>>  for (col in 1:9)
>>  {
>>  cinit=cinit+1
>>  st=cinit+1
>>  end=dim(d)[2]
>>  r=d[,cinit]-d[,st:end]
>>  r2=cbind(r2,r)
>>  }
>>
>> --
>> View this message in context: http://r.789695.n4.nabble.com/calculating-ratios-from-all-combinations-tp3912560p3912767.html
>> Sent from the R help mailing list archive at Nabble.com.
>>
>> ______________________________________________
>> 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