我需要创建一个新的列/变量,该列/变量仅包含一列具有许多可能值的字符串(例如,“ USA”,“ USA and Ecuador”和“ Switzerland”等国家名称) )
我尝试使用ifelse加上一些正则表达式来创建一个新列,该列将国家分类为“ WEIRD”或“ NonWEIRD”。该语法可以运行,但是会将所有值都设为“ NonWEIRD”(即ifelse fn。未找到任何真实结果)。
dataset$WEIRD<-ifelse(PhilCogCOR1$CITIZEN==".*Austria.*" |
PhilCogCOR1$CITIZEN==".*Belgium.*" |
PhilCogCOR1$CITIZEN==".*Canada.*" |
PhilCogCOR1$CITIZEN==".*Chile.*" |
PhilCogCOR1$CITIZEN==".*Czech Republic.*" |
PhilCogCOR1$CITIZEN==".*France.*" |
PhilCogCOR1$CITIZEN==".*Germany.*" |
PhilCogCOR1$CITIZEN==".*Hungary.*" |
PhilCogCOR1$CITIZEN==".*New Zealand.*" |
PhilCogCOR1$CITIZEN==".*Poland.*" |
PhilCogCOR1$CITIZEN==".*Portugal.*" |
PhilCogCOR1$CITIZEN==".*Spain.*" |
PhilCogCOR1$CITIZEN==".*Sweden.*" |
PhilCogCOR1$CITIZEN==".*Switzerland.*" |
PhilCogCOR1$CITIZEN==".*Netherlands.*" |
PhilCogCOR1$CITIZEN==".*United Kingdom.*" |
PhilCogCOR1$CITIZEN==".*USA.*",
"WEIRD",
"NonWEIRD")
如果此编码按预期工作,我将得到一列主要是“ WEIRD”值和一些“ NonWEIRD”值的