cdf cdf - 2 months ago 5
R Question

putting variables into vectors to be used in subsequent function (R)

i have a question regarding my second function which is not producing what i wanted to

#example for mt-cars
selected_cyl_6 <- subset(mtcars, mtcars$cyl==6)
selected_cyl_4 <- subset(mtcars, mtcars$cyl==4)

count <- function(variable,group) {
sum(group[[deparse(substitute(variable))]]== 4)
}
#now i would like to create a matrix with the results like here below:
#example
u<-count(gear,selected_cyl_6)
v<-count(carb,selected_cyl_6)
w<-count(gear,selected_cyl_4)
x<-count(carb,selected_cyl_4)

matrix(c(u,v,w,x), byrow=TRUE, ncol=2, nrow=2)


this is a feasible option, however i would love to convert the lines above into a function because in my dataset there are a lot more variables and i would save a lot of time and code length. i tried it with the following code:

variables_of_interest <- c("gear","carb")
groups_of_interest <- c("selected_cyl_6","selected_cyl_4")


alteration_multiple <- function(variable_vector, group_vector){
m.results <- matrix(0, nrow = length(variable_vector), ncol = length(group_vector))
rownames(m.results) <- variable_vector
colnames(m.results) <- group_vector
for (t in 1:length(group_vector)) {
for (i in 1:length(variable_vector)) {
m.results[i,t] <- count(group_vector[t],variable_vector[i])
}
print(m.results)
}
}
alteration_multiple(variable_vector=variables_of_interest, group_vector=groups_of_interest)


any suggestions? thank you so much for your support!

Answer

If you're going to use quoted strings to refer to data and variables, we need to modify your two functions.

The main task is to redefine the count function so that it can handle your proposed inputs. We can use eval and as.name to do this.

new count function

count2 <- function(variable,group) {
    sum(eval(as.name(group))[[variable]]== 4)
}

new alteration function

alteration_multiple2 <- function(variable_vector, group_vector){
    m.results <- matrix(0, nrow = length(variable_vector), ncol = length(group_vector))
    rownames(m.results) <- variable_vector
    colnames(m.results) <- group_vector
    for (t in 1:length(group_vector)) {
        for (i in 1:length(variable_vector)) {
            m.results[i,t] <- count2(variable_vector[i], group_vector[t])
        }
    }
    return(m.results)
}

result

variables_of_interest <- c("gear","carb")
groups_of_interest <- c("selected_cyl_6","selected_cyl_4")
alteration_multiple2(variable_vector=variables_of_interest, group_vector=groups_of_interest)

     selected_cyl_6 selected_cyl_4
gear              4              8
carb              4              0

Another way to define our alteration function

We can avoid a for loop and execute our function using matrix, apply, and expand.grid.

alteration_multiple3 <- function(variables, groups){ 
    matrix(
        apply(
        expand.grid(variables, groups), 1,
          function(x) count2(x[1],x[2])),
    nrow = length(variables),
    ncol = length(groups),
    dimnames = list(variables, groups))
    }
Comments