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

data frame rows separated by semicolon, how do I subtract numbers and add to new column?

I have a data frame where my rows in one column have numbers separated by a semicolon. How do I subtract the first number from the last number and add that value to a new column? Say my data frame looks like this below:

df <- data.frame(x=c("1;2;3","1;1;1;1"),y=c(5,5))

How do I add a new column of the subtracted values from x? So the new data frame looks like:

df <- data.frame(x=c("1;2;3","1;1;1;1"),y=c(5,5),z=c(2,0))

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 :

Try this with stringr. The first regular expression takes the last number from the string and the second one takes the first number and then you can just subtract them.

df <- data.frame(x=c("1;2;3","1;1;1:1"
                     ),y=c(5,5),z=c(2,0))

library(stringr)
df$eval <-   as.numeric(str_extract(df$x,"(\\d+$)")) -
             as.numeric(str_extract(df$x,"[0-9]+"))
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