juanjedi
juanjedi

Reputation: 160

Subset dataframe based on object containing variable name in R

I have an object (var) that is subject to change containing the name of any variable of a data frame df. I'm trying to create a loop that can subset based on this variable's values.

df[df$var == "value", 3]
for(l in levels(outcome){
   for(i in names(df)){
      list <- table(df[df$i == l, 3])
   }
 }

The above loop should return a list of tables of each variable in the data frame when that variable is equal to a particular value.

Take for example the following toy data set:

df <- data.frame(person, salary, haircolor, outcome)
head(df)
          person salary haircolor  outcome
1       John Doe  21000    black   married
2     Peter Gynn  23400    blonde  divorced
3     Jolie Hope  26800    blonde  divorced
4      Tom Hanks  40000    brown   married
5 Angelina Jolie  20330    brown   divorced
6      Peter Pan  23020    blonde  married

The expected output is the following, if i = outcome (but i can be anything here, this is the point) for all values of l == married:

>table(df[df$outcome == 'married', 3])
black blonde  brown 
     2      1      0 

However, I want to emphasize that the variable name and value is meant to be looped over every single one, so is up for change.

Upvotes: 1

Views: 196

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 388962

Maybe writing a function would help :

get_counts <- function(data, i, l) {
  table(data[data[[i]] == l, 3])
}

Then you can pass column name and value to subset from the dataframe and count using table

get_counts(df, 'outcome', 'married')

# black blonde  brown 
#     1      1      1 

Upvotes: 1

Related Questions