- 已关闭。**此问题需要debugging details。当前不接受答案。
编辑问题以包含desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将有助于其他人回答问题。
14小时前关门了。
Improve this question
我在把一些STATA代码翻译成R代码时遇到了一些麻烦:
状态代码:
gen joint_gpw = sbud_jpw * q44 if sbud_jpw < 888 & q44 < 888
gen sbud_gpw_all = sbud_gpw if sbud_gpw < 888
replace sbud_gpw_all = q31 if sbud_gpw_all ==. & q31 < 888
replace sbud_gpw_all = joint_gpw if sbud_gpw_all ==. & joint_gpw !=.
replace sbud_gpw_all = 888 if q16_1 == 0 & sbud_gpw_all ==.
replace sbud_gpw_all = 888 if (sbud_gpw == 888 & q31 == 888 & sbud_jpw == 888 & q44 == 888) & sbud_gpw_all ==.
replace sbud_gpw_all = 999 if (sbud_gpw == 999 | q31 == 999 | sbud_jpw == 999 | q44 == 999 | (q44 !=. & sbud_jpw == 888)) & sbud_gpw_all ==.
下面是我尝试的R代码:
dat%>%
dplyr::mutate(joint_gpw = ifelse((sbud_jpw<888 & q44<888),sbud_jpw * q44,NA))%>%
dplyr::mutate(sbud_gpw_all = ifelse(sbud_gpw < 888,sbud_gpw,NA))%>%
dplyr::mutate(sbud_gpw_all = ifelse((sbud_gpw_all= NA & q31<888),q31,NA))%>%
dplyr::mutate(sbud_gpw_all = ifelse((sbud_gpw_all = NA & joint_gpw != NA),joint_gpw,NA))%>%
dplyr::mutate(sbud_gpw_all) = ifelse((q16_1 = 0 & sbud_gpw_all = NA),888,NA)%>%
dplyr::mutate(sbud_gpw_all) = ifelse((sbud_gpw = 888 & q31 = 888 & sbud_jpw = 888 & q44 = 888) & sbud_gpw_all = NA,888,NA)%>%
dplyr::mutate(sbud_gpw_all) = ifelse(((sbud_gpw = 999 | q31 = 999 | sbud_jpw = 999 | q44 = 999 | (q44 != NA & sbud_jpw == 888)) & sbud_gpw_all = NA)),999,NA)
以前出现的错误:
Error: unexpected '=' in:
" dplyr::mutate(sbud_gpw_all) = ifelse((q16_1 = 0 & sbud_gpw_all = NA),888,NA)%>%
dplyr::mutate(sbud_gpw_all) = ifelse((sbud_gpw = 888 & q31 = 888 & sbud_jpw = 888 & q44 = 888) & sbud_gpw_all ="
我想知道,如果这两套代码是等效的?我非常感谢所有的帮助有!谢谢!!!
1条答案
按热度按时间yvfmudvl1#
错误源于最后三行中
sbud_gpw_all
后面的右括号)
。另外,尽管没有抛出错误,但每次变异都覆盖了
sbud_gpw_all
。我不知道Stata,你也没有提供一个最小的可重复示例,但我感觉你的代码可以像这样工作:这将首先使用
dplyr::if_else()
ifsbud_jpw < 888 & q44 < 888
创建列joint_gpw
。然后,有一组条件(在~
之前)被顺序检查。第一个匹配行的条件提供值(在~
运算符之后)。注意,正如Sotos在注解中指出的,R中的
NA
是用is.na(x)
检查的,而不是用==
/!=
,因为它们总是返回NA
。我省略了大多数行的NA
检查,因为case_when()
的顺序特性中隐含了这些检查--只要一个条件匹配,NA_real_
是一个数字NA
值,使用if_else()
和case_when()
时,必须明确数据类型。