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

How to subtrate a diferent value in all columns in R

I got this data.frame

Data = data.frame(    Time = seq(1,5),
                    v1 = c(0.1, 0.2, 0.4, 0.7, 0.9),
                    v2 = c(0.1, 0.12, 0.41, 0.72, 0.91),
                    v3 = c(0.03, 0.13, 0.62, 0.50, 0.90))

but i need to subtrate a certain value in all columns

subtrate = data.frame(v1 = 0.1, v2 = 0.3, v3 = 0.5)

To get this result:

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

result =

   v1    v2    v3
1 0.0 -0.20 -0.47
2 0.1 -0.18 -0.37
3 0.3  0.11  0.12
4 0.6  0.42  0.00
5 0.8  0.61  0.40

>Solution :

With purrr you can try:

map2_df(Data[-1], subtrate, `-`)

Output

     v1    v2    v3
  <dbl> <dbl> <dbl>
1   0   -0.2  -0.47
2   0.1 -0.18 -0.37
3   0.3  0.11  0.12
4   0.6  0.42  0   
5   0.8  0.61  0.4 
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