简体   繁体   中英

How to subtract a value from one column based on the value of another in R?

I have a dataframe with a list of IDs of 1-2000 and each one of these IDs has multiple collections timepoints (each new collection is a new row). I have removed timepoint 1 for those with an ID of less than 1177. What I want to do now is to subtract 1 from the remaining collections for all samples that are less than 1177. For example, collection 2 will become 1 and 3 will become 2. What's the easiest way to do this?

I think that some code and an example df would help us to understand. However, you may do something like this

df$collection <- ifelse(df$ID > 1177, df$collection - 1, df$collection )

This only works if collection is a numerical vector. df is your dataframe

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM