?? Any R script for subtracting multiple controls from multiple patients in data.frame and assign 1 if output is less than 0
0
0
Entering edit mode
Ankit ▴ 20
@ankit-21750
Last seen 7 months ago
Italy

Hi everyone.

I have a data.frame

ID  control1    control2    control3    patient1    patient2    patient3
ref1    5   6   9   3   2   2
ref2    6   5   8   2   3   3
ref3    5   6   7   3   2   2
ref4    5   5   6   6   8   6
ref5    4   6   6   7   6   5
ref6    5   7   8   2   3   1
ref7    6   7   7   1   3   2
ref8    6   8   7   6   9   8
ref9    7   9   8   1   1   2
ref10   7   9   8   8   9   8

I want to subtract all controls from all patients. For example patient1 - control1 , patient2-control1, pateint3-control1 and so on for other combinations. If the output value is less than 2, then assign number 1 to it. At the same time or after this I want to sum all the 1 to get total number of output values which are are less than 2. So for example if p1-c1, p2-c1,p3-c1 is all less than 2 then it will be 1+1+1 =3. I want dataframe as an output of final sum.

I tried doing this with forloop in R, but could not do it (just started learning looping in R).

my attempt:

controls <- file[,1:3]
patients <- file[,4:6]


for (i in controls){
  for (j in patients){
    print(j -i)
  }
}

This print 9 possibilities that too in transposed frame wise. But what do after this.

I did it manually for this example data using excel sheets to explain better:

9 possibilities

ID  p1-c1   p1-c2   p1-c3   p2-c1   p2-c2   p2-c3   p3-c1   p3-c2   p3-c3
ref1    -2  -3  -6  -3  -4  -7  -3  -4  -7
ref2    -4  -3  -6  -3  -2  -5  -3  -2  -5
ref3    -2  -3  -4  -3  -4  -5  -3  -4  -5
ref4    1   1   0   3   3   2   1   1   0
ref5    3   1   1   2   0   0   1   -1  -1
ref6    -3  -5  -6  -2  -4  -5  -4  -6  -7
ref7    -5  -6  -6  -3  -4  -4  -4  -5  -5
ref8    0   -2  -1  3   1   2   2   0   1
ref9    -6  -8  -7  -6  -8  -7  -5  -7  -6
ref10   1   -1  0   2   0   1   1   -1  0

Assigned 0 if > 0 or 1 if < than 0

ID  p1-c1   p1-c2   p1-c3   p2-c1   p2-c2   p2-c3   p3-c1   p3-c2   p3-c3
ref1    1   1   1   1   1   1   1   1   1
ref2    1   1   1   1   1   1   1   1   1
ref3    1   1   1   1   1   1   1   1   1
ref4    0   0   1   0   0   0   0   0   1
ref5    0   0   0   0   1   1   0   1   1
ref6    1   1   1   1   1   1   1   1   1
ref7    1   1   1   1   1   1   1   1   1
ref8    1   1   1   0   0   0   0   1   0
ref9    1   1   1   1   1   1   1   1   1
ref10   0   1   1   0   1   0   0   1   1

Sum the possibilities as per the patients

ID  p1  p2  p3
ref1    3   3   3
ref2    3   3   3
ref3    3   3   3
ref4    1   0   1
ref5    0   2   2
ref6    3   3   3
ref7    3   3   3
ref8    3   0   1
ref9    3   3   3
ref10   2   1   2

calculate percentage of output*100/3

ID  p1  p2  p3
ref1    100.0   100.0   100.0
ref2    100.0   100.0   100.0
ref3    100.0   100.0   100.0
ref4    33.3    0.0 33.3
ref5    0.0 66.7    66.7
ref6    100.0   100.0   100.0
ref7    100.0   100.0   100.0
ref8    100.0   0.0 33.3
ref9    100.0   100.0   100.0
ref10   66.7    33.3    66.7

But my original My data.frame is large 300K with multiple controls and cases. I prefer for loop in R. Please help.

Thank you

Rscript data.frame forloop R • 763 views
ADD COMMENT
0
Entering edit mode

Hi, does this relate to any Bioconductor package? This support site is specifically for Bioconductor packages.

ADD REPLY

Login before adding your answer.

Traffic: 537 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6