ila
ila

Reputation: 766

Is there a way to create multiple new variables in a dataframe at once, using data from existing variables?

I have a dataset, df, with a series of variables which measure the frequency of some occurrence in any given case. I also have as an object a list of these variables, let's call them c(v1, v2, v3, v4, v5). For each of these variables, I wish to create a new variable in the dataset which is a binary indicator of whether the frequency measurement is greater than zero. This could be done manually pretty easily:

df$v1binary <- 0
df$v1binary[df$v1 > 0] <- 1
#etc for each variable

However, my variable list is very large and dynamic, so I'd like to find a more easily replicable solution. Is there a way to do this? Perhaps using a for-loop?

Here's some data to play with:

df <- data.frame(CaseID = seq(1,10,1), v1 = sample(0:9), v2 = sample(0:9), v3 = sample(0:9), v4 = sample(0:9), v5 = sample(0:9))

varlist <- c("v1", "v2", "v3", "v4", "v5")

Upvotes: 0

Views: 1433

Answers (1)

mnel
mnel

Reputation: 115392

Simple use case for lapply and [<-.data.frame

 df[paste0(varlist,'binary')] <- lapply(df[varlist], function(x) as.integer(x>0))

Upvotes: 2

Related Questions