I am new to writing loops and have been trying hard to figure this out myself.
I have a dataframe where a unique threshold needs to be applied over each column individually. If the value is higher than the threshold, then the original value is kept. If it is lower, it will be converted to 0.
This is just a trimmed down dataframe Data.t as an example.
Peptide.1 Peptide.2 Peptide.3
MF.001 -0.01 0.04 0.04
MF.006 0.02 0.05 -0.02
MF.007 0.03 0.05 0.06
MF.015 0.00 0.04 0.05
QEM.219 -0.02 0.02 0.01
QEM.234 0.41 0.15 0.02
QEM.277 -0.04 0.00 0.03
QEM.409 0.00 0.00 0.00
And this is the individual thresholds as a separate data frame threshold.
V1 V2 V3
threshold 0.236 0.172 0.176
I have figured out the first part using an ifelse function.
Data.n$Peptide.1 <- ifelse(Data.t$Peptide.1 > threshold$V1, Data.t$Peptide.1, 0)
This would now give me:
Peptide.1
MF.001 0.00
MF.006 0.00
MF.007 0.00
MF.015 0.00
QEM.219 0.00
QEM.234 0.41
QEM.277 0.00
QEM.409 0.00
But now, I would like to apply the individual thresholds to all columns, ie threshold$V2 to Data.t$Peptide.2 and so on. I know I can do this individually, but I have 84 columns in totals. I will also have many more of these dataframes in the future, so I thought it will be helpful to write a loop.
Could I please get some help with this?
I am sure it's a super easy thing, but I tried writing lots of different for loops and all failed miserably.
Thanks so much!
Aucun commentaire:
Enregistrer un commentaire