为什么 rowSums 对某些变量起作用而对其他变量不起作用?
Why does rowSums work for some variables and doesn't vork for others?
我正在使用以下代码行从其他变量创建新变量:
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
问题是,虽然它对某些列工作正常,但对于其他列,我收到以下错误:
FACE.RESP + RESPOND.RESP 错误:
二元运算符的非数字参数
所有 columns/variables 都包含 NA 值,我不明白为什么代码针对某些变量而不是其他变量执行。有任何修复建议吗?
数据结构:
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")
根据 dput
输出,其中两列不是 numeric
,即 FACE.RT
和 RESPOND.RESP
。我们可以使用 mutate_at
将它们转换为 numeric
,现在代码可以工作了
FER_TMS_nopractice_recoded_replaced <- FER_TMS_nopractice_recoded_replaced %>%
mutate_at(vars(FACE.RT, RESPOND.RESP), as.numeric)
我正在使用以下代码行从其他变量创建新变量:
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
问题是,虽然它对某些列工作正常,但对于其他列,我收到以下错误: FACE.RESP + RESPOND.RESP 错误: 二元运算符的非数字参数
所有 columns/variables 都包含 NA 值,我不明白为什么代码针对某些变量而不是其他变量执行。有任何修复建议吗?
数据结构:
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")
根据 dput
输出,其中两列不是 numeric
,即 FACE.RT
和 RESPOND.RESP
。我们可以使用 mutate_at
将它们转换为 numeric
,现在代码可以工作了
FER_TMS_nopractice_recoded_replaced <- FER_TMS_nopractice_recoded_replaced %>%
mutate_at(vars(FACE.RT, RESPOND.RESP), as.numeric)