Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Asigning a rank (lowest value to highest) to a numeric value in one column based on an identifier in another column

I would need help asigning a rank (lowest value to highest) to some numeric values in one column. The values that should be included in this have an identifier in a different column (Here Significant = Yes).

I want to add a new column where I rank numeric values by their fold change ("log2FC") with the identifier Significant=Yes in the Significant column.

I would appreciate the help.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

#initial data

treatment=c(100,200,100,400)
cotrol=c(100,100,400,100)
log2FC=c(0,1,-2,2)
Significance=c(No,Yes,Yes,Yes)
data=data.frame(treatment,cotrol,log2FC,Significance)
treatment cotrol log2FC Significance
100 100 0 No
200 100 1 Yes
100 400 -2 Yes
400 100 2 Yes
treatment cotrol log2FC Significance Rank
100 100 0 No
200 100 1 Yes 2
100 400 -2 Yes 1
400 100 2 Yes 3

>Solution :

Use rank on the subset of rows:

data$Rank[data$Significance == "Yes"] <- rank(data$log2FC[data$Significance == "Yes"])

output

  treatment cotrol log2FC Significance Rank
1       100    100      0           No   NA
2       200    100      1          Yes    2
3       100    400     -2          Yes    1
4       400    100      2          Yes    3
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading