-1

I have typical survey style data in R. Multiple columns with binary -- yes/no -- records. I'm looking to create a single, categorical column based on those columns.

We'll call the dataframe schools

    PreK  Kindergarten First Second Third Fourth ... 
1    yes      yes       no     no    no    no 
2    no       no        yes    yes   yes   yes 

I'd like the new column to look like this

                                         ...       Grades
1                                                 prek, kg
2                                                 elementary

I thought it might need to have to be a lot of nested ifelse strings. It goes up to 14 columns, and the new "grades" column will need to be diverse enough to address any combination of grades (e.g. 6 - 12).

I'm trying to create a new column based on the values of already existing columns. Those existing columns are all YES and NO (like checkmarks). I want the new column to reflect categories that bin together the YES values. Say, one row has YES in the "ninth", "tenth", "eleventh" and "twelfth" column, I want that to translate into "high school" in the new column.

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
rip_ryan
  • 3
  • 4
  • Hi. Your question is a bit unclear. - (I) Can you provide a small example that people can replicate? - (II) What is that new column you want to, the words you write in there hare different from the names of the data frame you defined first. - (III) Is the column supposed to be a new column of the same data.frame? – Daniel Jul 19 '16 at 20:03
  • Sorry about that. I'm trying to create a new column based on the values of already existing columns. Those existing columns are all YES and NO (like checkmarks). I want the new column to reflect categories that bin together the YES values. Say, one row has YES in the "ninth", "tenth", "eleventh" and "twelfth" column, I want that to translate into "high school" in the new column. – rip_ryan Jul 19 '16 at 20:09
  • Great! Please add that description to your question above by editing it – Daniel Jul 19 '16 at 20:11
  • Possible duplicate of [Call apply-like function on each row of dataframe with multiple arguments from each row](http://stackoverflow.com/questions/15059076/call-apply-like-function-on-each-row-of-dataframe-with-multiple-arguments-from-e) – David Jul 19 '16 at 20:17
  • The new column needs to be original categories (like "elementary" or "high school") Yes the column is supposed to be of the data.frame – rip_ryan Jul 19 '16 at 20:19

1 Answers1

0

We can use apply with MARGIN =1 to loop over the rows

df1$Grades <-  apply(df1, 1, FUN = function(x) {
      i1 <- x=="yes"
      if(any(i1[-(1:2)])) "elementary" else toString(tolower(names(df1)[i1]))})

data

df1 <- structure(list(PreK = c("yes", "no"), Kindergarten = c("yes", 
"no"), First = c("no", "yes"), Second = c("no", "yes"), Third = c("no", 
"yes"), Fourth = c("no", "yes")), .Names = c("PreK", "Kindergarten", 
"First", "Second", "Third", "Fourth"), class = "data.frame", 
row.names = c("1", "2"))
akrun
  • 874,273
  • 37
  • 540
  • 662