-2

I am using the following lines of code for creating a new variable from other variables:

FER_TMS_nopractice_recoded_replaced %>% select(FACE.RT, SCRAM1.RT_total, SCRAM2.RT_total, SCRAM3.RT_total, 
                             SCRAM4.RT_total, SCRAM5.RT_total, SCRAM6.RT_total, SCRAM7.RT_total, SCRAM8.RT_total, 
                             SCRAM9.RT_total, SCRAM10.RT_total, SCRAM11.RT_total, SCRAM12.RT_total, RESPOND.RT_total, 
                             WHITE4.RT_total) %>% rowSums(na.rm = TRUE) -> FER_TMS_nopractice_recoded_replaced$RT_TOTAL

The problem is that while it works fine for some columns, for others I get the following error: Error in FACE.RESP + RESPOND.RESP : non-numeric argument to binary operator

All the columns/variables contain NA values and I do not understand why the code executes for some and not other variables. Any suggestions for a fix?

Data Structure:

structure(list(SessionDate = structure(c(1L, 1L, 1L), .Label = c("07-16-2019", 
"07-23-2019", "07-30-2019"), class = "factor"), SessionTime = structure(c(1L, 
1L, 1L), .Label = c("11:46:03", "12:09:32", "13:12:03", "13:36:15", 
"17:01:32", "17:28:27"), class = "factor"), Subject = c(3L, 3L, 
3L), Sex = structure(c(1L, 1L, 1L), .Label = "male", class = "factor"), 
    Age = c(23L, 23L, 23L), Handedness = structure(c(1L, 1L, 
    1L), .Label = "right", class = "factor"), preOrpost = c("PRE", 
    "PRE", "PRE"), Site = c("RIGHT", "RIGHT", "RIGHT"), Condition = structure(c(2L, 
    2L, 2L), .Label = c("EMOPRAC", "EMOstim", "GENDPRAC", "GENDstim"
    ), class = "factor"), Duration = c(300L, 40L, 40L), Correct = c(2L, 
    2L, 1L), FACE.RESP = c("1", "1", "1"), SCRAM1.RESP = c(NA, 
    NA, NA), SCRAM2.RESP = c(NA, NA, NA), SCRAM3.RESP = c(NA, 
    NA, NA), SCRAM4.RESP = c(NA, NA, NA), SCRAM5.RESP = c(NA, 
    NA, NA), SCRAM6.RESP = c(NA, NA, NA), SCRAM7.RESP = c(NA, 
    NA, NA), SCRAM8.RESP = c(NA_character_, NA_character_, NA_character_
    ), SCRAM9.RESP = c(NA_character_, NA_character_, NA_character_
    ), SCRAM10.RESP = c(NA_character_, NA_character_, NA_character_
    ), SCRAM11.RESP = c(NA_character_, NA_character_, NA_character_
    ), SCRAM12.RESP = c(NA_character_, NA_character_, NA_character_
    ), RESPOND.ACC = c(1L, 0L, 1L), RESPOND.RESP = c("1", "0", 
    "1"), WHITE4.RESP = c(NA, NA, NA), FACE.RT = c(NA_integer_, 
    NA_integer_, NA_integer_), SCRAM1.RT = c(NA_integer_, NA_integer_, 
    NA_integer_), SCRAM2.RT = c(NA_integer_, NA_integer_, NA_integer_
    ), SCRAM3.RT = c(NA_integer_, NA_integer_, NA_integer_), 
    SCRAM4.RT = c(NA_integer_, NA_integer_, NA_integer_), SCRAM5.RT = c(NA_integer_, 
    NA_integer_, NA_integer_), SCRAM6.RT = c(NA_integer_, NA_integer_, 
    NA_integer_), SCRAM7.RT = c(NA_integer_, NA_integer_, NA_integer_
    ), SCRAM8.RT = c(NA_integer_, NA_integer_, NA_integer_), 
    SCRAM9.RT = c(NA_integer_, NA_integer_, NA_integer_), SCRAM10.RT = c(NA_integer_, 
    NA_integer_, NA_integer_), SCRAM11.RT = c(NA_integer_, NA_integer_, 
    NA_integer_), SCRAM12.RT = c(NA_integer_, NA_integer_, NA_integer_
    ), RESPOND.RT = c(251L, 1111L, 426L), WHITE4.RT = c(NA_integer_, 
    NA_integer_, NA_integer_)), row.names = c(NA, 3L), class = "data.frame")
  • 2
    THe error message is iinformative in this case i.e. you can check the `str(FER_TMS_nopractice_recoded_replaced)` and see if there are non-numeric columns included in the `rowSums` calculation – akrun Jan 19 '20 at 21:41
  • They are all numeric with a lot of NA values though. – Eva Balgova Jan 19 '20 at 21:47
  • In that case, the code should work. Here, the error is very specific as it encountered a non-numeric column type. If you can show a small `dput` output of the first 3 rows, it would be eassier to test i.e. `dput(head(FER_TMS_nopractice_recoded_replaced, 3))` – akrun Jan 19 '20 at 21:48
  • I would also check `class(FER_TMS_nopractice_recoded_replaced$FACE.RESP)` and the `RESPOND.RESP` – akrun Jan 19 '20 at 21:50
  • When printing the output of first rows, I noticed that some NA values are coded simply as NA, but some are as NA_integrer and NA_character. Would that be the problem? The code works with NA_integrer columns and not others that contain NA or NA_character. – Eva Balgova Jan 19 '20 at 22:03
  • 1
    It means that for the columns with `NA_character` it is a `character` classs column. Convert it to `numeric` with `as.numeric` and it should work i.e. `as.numeric(FER_TMS_nopractice_recoded_replaced$yourcolumn)` – akrun Jan 19 '20 at 22:04
  • Converting the columns with as.numeric does not help. Still the same error. – Eva Balgova Jan 19 '20 at 22:23
  • 1
    As I mentioned earlier, please consider to copy paste the `dput(head(yourdata))` in your post so that we get the structure of your data – akrun Jan 19 '20 at 22:25
  • dput(head(yourdata)) output copied to the main text. I only use columns ending .RT and .RESP. It works for .RT columns but not for .RESP – Eva Balgova Jan 19 '20 at 22:33
  • 1
    In your dput, some of the columns are `character` class and also the `_total` columns are not there – akrun Jan 19 '20 at 22:39

1 Answers1

3

Based on the dput output, two of the columns are not numeric i.e. FACE.RT and RESPOND.RESP. We can convert them to numeric with mutate_at and now the code would work

FER_TMS_nopractice_recoded_replaced <- FER_TMS_nopractice_recoded_replaced  %>% 
                          mutate_at(vars(FACE.RT, RESPOND.RESP), as.numeric)
akrun
  • 874,273
  • 37
  • 540
  • 662