Renato Vieira Renato Vieira - 2 months ago 5x
R Question

R - How to assign the value of a variable based on another variable which is related to column names of a data frame?

I have a data frame with the following variables:

df <- data.frame(ID = seq(1:5),
Price.A = c(10,12,14,16,18),
Price.B = c(6,7,9,8,5),
Price.C = c(27,26,25,24,23),
Choice = c("A", "A", "B", "B", "C"))

I want to create a variable called Expenditure, which picks the value from Price.A, Price.B or Price.C depending on the value of the variable Choice.

I tried to create it with the following code:

df$Expenditure <- with(df, get(paste("Price.", Choice, sep ="")))

However, that returns the value of Price.A for all observations.

In my real application, instead of A, B and C, I have hundreds of names, so an ifelse command is not feasible.

Does anyone knows how to do that?

Thank you!


Here are a couple of *apply based approaches:

df$Expenditure <- sapply(seq_along(df[[1]]), function(i) { 
    df[i, sprintf("Price.%s", df$Choice[i])] 

df$Expenditure <- mapply(function(x, y) {
        df[x, sprintf("Price.%s", y)]
    }, row.names(df), df$Choice

The second one assumes your object has the default row.names of 1:nrow(df).