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

Conditional subtraction in R data frame

I have a fairly straightforward need, but I can’t find a previously asked question that is similar enough. I’ve been trying with dplyr, but can’t figure it out.

 julian year
   088   22
   049   19
   041   22
   105   18
   125   22
   245   20

What I want is for each value where data$julian < 105, subtract ‘1’ from data$year, so that

 julian year
   088   21
   049   18
   041   21
   105   18
   125   22
   245   20

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

>Solution :

Another option with base R:

df$year[df$julian < 105] <- df$year[df$julian < 105] - 1

Output

  julian year
1    088   21
2    049   18
3    041   21
4    105   18
5    125   22
6    245   20

Data

df <- structure(list(name = c("KKSWAP", "KKSWAP"), code = c("The liquidations code for Marco are: 51-BMR05, 74-VAD08, 176-VNF09.", 
"The liquidations code for Clara are: 88-BMR05, 90-VAD08, 152-VNF09."
)), class = "data.frame", row.names = c(NA, -2L))
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