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

Time:09-12

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.

#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

CodePudding user response:

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
  •  Tags:  
  • r
  • Related