How do I create a new column based on whether the values in other columns are equal to specific strings?

40 Views Asked by At

I'm new to coding and R but wanted to use it for the data analysis in my masters research project.

I'm hoping to create a new column based on whether or not the values in other columns on that row are equivalent to specific values. I've tried multiple methods but haven't had any success and have hit a block troubleshooting.

Method 1: Nested for loop

    y <- 1
    for (i in Scores$PCLVL){
      for (j in Scores$PSLVL){
        if (i == "High" && j == "High"){
          Scores$Perf[y] <- "Mixed Perfectionism"
          y <- y + 1
        } else if (i == "Low" && j == "High"){
            Scores$Perf[y] <- "PSP"
            y <- y + 1
          }
          else if (i == "High" && j == "Low"){
            Scores$Perf[y] <- "ECP"
            y <- y + 1
          }
          else if (i == "Low" && j == "Low"){
            Scores$Perf[y] <- "Non Perfectionism"
            y <- y + 1
          }
      }
    }

Method 2: If statement equivalency

    Scores$Perf <- if (Scores$PCLVL == "High" & Scores$PSLVL == "High", 'Mixed 
Perfectionism',
                       if else (Scores$PCLVL == "High" & Scores$PSLVL == "Low", 'ECP',
                       if else (Scores$PCLVL == "Low" & Scores$PSLVL == "High", 'PSP', 
'Non Perfectionism'
                       if else (Scores$PCLVL == "Low" & Scores$PSLVL == "Low", 'Non Perfectionism', 'NA'))))

The data frame begins looking like this:

  PCLVL PSLVL
1  High  High
2  High  High
3   Low  High
4  High  High
5  High  High
6   Low  High

I want it to look like this:

  PCLVL PSLVL       PERF
1  High  High Mixed Perfectionism
2  High  Low        ECP
3   Low  High       PSP
4  High  High Mixed Perfectionism
5   Low  Low   Non Perfectionism
6   Low  High       PSP
1

There are 1 best solutions below

1
kjetil b halvorsen On

It is not clear what you want, but maybe try ifelse(). Something like

Scores <- within(Scores, {PERF <- ifelse(
   (PCLVL=="High")&(PSLVL=="High"), "Mixed Perfectionism", 
    ifelse((PCLVL=="High")&(PSLVL=="Low"), "ECP", 
    ifelse((PCLVL=="Low")&(PSLVL=="High"), "PSP", 
    ifelse( ... 

I left the code unfinished, continue in same way.

Note that if ... is for code logic, and do not operate on the rows of dataframes. ifelse function is the vectorialized version which does.