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 transform a set of numbers in percentages

Let’s assume I have these numeric objects (observations), obtained from 2 separate experiments

Control <- 50 
A <- 25
B <- 5
Experiment_1 <- c(Control , A , B)

Control_2 <- 70
A2 <- 23.3
B2 <- 140
Experiment_2 <- c(Control_2 , A2 , B2)

For each experiment, I want the numeric objects to be transformed in percentages, with the specific control value being the 100% and the other numbers following the same proportion. For instance, Experiment_1 and 2 should become:

Control = 100%
A = 50%
B = 10%

Control_2 = 100%
A2 = 30%
B2 = 200%

What script can do that? I believe it’s a simple proportion applied to each number (50=100% , 50:100%=25:X , 50:100%=5:X etc…) but I have no idea how to do that practically, and having a string for each number is a tad too bothersome, I’m sure there is a shorter way to obtain the same values.

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

If possible, could you please use base R commands? I am not well versed in many packages or coding in general and I prefer simple albeit not very elegant scripts so I can learn from the basics.
Thank you!

>Solution :

How about this?

Control <- 50 
A <- 25
B <- 5
Experiment_1 <- c("Control"=Control ,"A"= A ,"B"= B)
(Experiment_1/Experiment_1["Control"])*100

This will return a named vector with the percentages. You can proceed similarly with your over experiments.

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