+1 vote
in R Language by
How to use nested if - else-if statement to change certain values in one column of dataframe based on criteria in another column?

1 Answer

0 votes
by
I'm importing participant data from an Excel spreadsheet and then am trying to change the values of certain cells in one column of the dataframe based on a criteria in another column (in the form of a nested if - else if statement inside an if statement).
However, I get the error message 'Error in if (.) Reverse_keyed == 1 else { : argument is not interpretable as logical'. 'Reverse_keyed' is the column heading - where the value is 1 (the other possible value it takes in the filtered cells is 0), I am trying to change the values in the 'response_responseV' column, depending on the values in those cells. 'D' is the master frame with each subject's data and variables.

questionnaire = D %>%
  filter(!is.na(response_responseV), is.na(response_responseS))  %>%
         if (Reverse_keyed == "1") {
          if (response_responseV == "1") {
            response_responseV == "7"
          } else if (response_responseV == "2") {
              response_responseV == "6"
            } else if (response_responseV == "3") {
                response_responseV == "5"
              } else if (response_responseV == "5") {
                  response_responseV == "3"
              } else if (response_responseV == "6") {
                  response_responseV == "2"
              } else if (response_responseV == "7") {
                  response_responseV == "1"
                }
        }     

Related questions

0 votes
asked Dec 30, 2019 in R Language by sharadyadav1986
0 votes
asked Nov 5, 2022 in Security Assessment by Robin
...