R If-Else语句并删除NA';s

R If-Else语句并删除NA';s,r,date,if-statement,R,Date,If Statement,我使用一个ifelse语句告诉程序将月份划分为各自的季度。例如1月至3月(一)、4月至6月(二)……等等。我使用以下ifelse语句: eight$quarter<-ifelse(eight$month=="01", eight$quarter=="I", 0) 8$quarter您可以尝试: eight$quarter<-ifelse(eight$month=="01", "I", 0) earth$quarter是“01”文本,没有错误地使用字母O?您分配了两次earth$q

我使用一个
ifelse
语句告诉程序将月份划分为各自的季度。例如1月至3月(一)、4月至6月(二)……等等。我使用以下
ifelse
语句:

eight$quarter<-ifelse(eight$month=="01", eight$quarter=="I", 0)
8$quarter您可以尝试:

eight$quarter<-ifelse(eight$month=="01", "I", 0)

earth$quarter是“01”文本,没有错误地使用字母O?您分配了两次
earth$quarter
尝试阅读
?如果其他
文档,问题是
earth$quarter==“I”
始终返回
FALSE
。他使用了
=
而不是
=
FYI,你可以避免使用
ifelse
,做一些类似
c(“0”,“I”)[(八个月==“01”)+1]
。此外,如果您不想得到
1
0
(而不是
I
0
)的向量,您可以简单地执行
(八美元月==“01”)+0
,您可以使用
八美元季度稍微减少代码