'R : Loop to keep only one specific value
I have a dataset and I would like to keep the value in a column of this dataframe (test_masses) for the mass having the highest intensity for masses close to the rounding of 2.
I created a loop but by observing the result of the dataframe, I see that this one is not optimal. Especially at values 95 and others
Here is the code:
data_frame = read.csv(file = "/home/julien/Bureau/test/File_Name.csv" )
data_frame$test_masses = 'NaN'
df <-data_frame[order(data_frame$masses),]
for (i in 1:(nrow(df)-1)) {
if( round(df[i,"masses"], digits = 2) ==
round(df[i+1,"masses"], digits = 2) & df[i,"intensities"] >
df[i+1,"intensities"])
{
df[i,'test_masses'] <- df[i,'masses']
}
}
You can find an output of the image too.
Taking the example for this image, I would therefore like the line with the highest intensity (line 6) to be written on the test_masses column
Finally here is the drive link of the dataframe :
https://drive.google.com/file/d/1YpVxD54V1rUyidivyTyF92ssXdHHAPge/view?usp=sharing
Thank you for your answers
Solution 1:[1]
Ok i find it guys
for (i in 1:(nrow(df)-1)) {
if( round(df[i,"masses"], digits = 2) == round(df[i+1,"masses"], digits = 2) & df[i,"intensities"] == df[i+1,"intensities"])
{
df[i+1,'intensities'] <- df[i+1,'intensities'] - 1
}
}
ok = df %>% group_by(round(masses , digits = 2)) %>% top_n(1, intensities)
names(ok)[names(ok) == "round(masses, digits = 2)"] <- "rounded"
ok = ok[!duplicated(ok$rounded),]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Julien Sade |

