aju_k aju_k - 3 months ago 21
R Question

specifying conditions as a variable to assign values in R

I have a dataframe to which I would like to assign percentages and reasons based on conditions:

attach(df)
df$profile[ColumnX>=0.5, ColumnY == 2]<-"Profile 1"
df$prediction[ColumnX>=0.5, ColumnY == 2]<-0.84
df$reason[ColumnX>=0.5, ColumnY == 2]<-"Reason 1"
detach(df)


Can I avoid the duplication by using a variable for "ColumnX>=0.5, ColumnY == 2"?

I tried the following, but it is not working:

var <- expression(ColumnX>=0.5, ColumnY == 2)
attach(df)
df$profile[eval(var)]<-"Profile 1"
df$prediction[eval(var)]<-0.84
df$reason[eval(var)]<-"Reason 1"
detach(df)


Any help would be appreciated.

Answer

You could create a logical vector with your condition, and then subset based on this.

# sample data
df <- data.frame(X=runif(100), Y=sample(1:4, 100, replace = TRUE))

# create a logical vector with the conditions
var <- df$X >= 0.5 & df$Y == 2

# assign new values
df$profile[var] <- "Profile1"
df$prediction[var] <- 0.84