user5193514
user5193514

Reputation:

Creating new variable using functions

I want to create new variables as follows:

MainModFinal$xAdAware_CA_1<-(MainModFinal$AdAware_CA_1/MainModFinal$ParticipantCount)*100
MainModFinal$xAdAware_CB_1<-(MainModFinal$AdAware_CB_1/MainModFinal$ParticipantCount)*100

So, I would like to divide the existing variable by the same Participant count and thus create a percentage variable out of it.

Can some one tell me how could it be accomplished as I need to repeat it for several more variables?

Upvotes: 0

Views: 62

Answers (1)

josliber
josliber

Reputation: 44309

If you had a vector of all the variables of interest:

vars <- c("Sepal.Length", "Sepal.Width", "Petal.Length")

Then you could update them all in one shot with:

iris[,vars] <- iris[,vars] / iris$Petal.Width * 100

Here, iris is the name of your data frame (and a convenient example data frame built into R) and Petal.Width is the variable you want to normalize by.

Upvotes: 1

Related Questions