Home > Mobile >  How to transform a set of numbers in percentages
How to transform a set of numbers in percentages

Time:12-06

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.

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!

CodePudding user response:

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.

  • Related