[英]error in if statement in r-missing value where TRUE/FALSE needed
I build this function in order to implement left_join in the "hard way".我构建了这个 function 以便以“硬方式”实现 left_join。 this is my code:
这是我的代码:
combined_2_df=function(df1,df2){
my_df<-df1
my_df$engine<-NA
i=1
while(i<=nrow(my_df)){
for(j in 1: nrow(df2))
{
if (my_df$year[i]==df2$year[j])
{
my_df$engine[i]=df2$engine[j]
break
}#if
}#for
i=i+1
}#while
return(my_df)
}
however I got an error:但是我得到了一个错误:
Error in if (my_df$year[i] == df2$year[j]) { :
missing value where TRUE/FALSE needed
what I do wrong?我做错了什么? Thank you
谢谢
Your error message你的错误信息
Error in if (my_df$year[i] == df2$year[j]) { :
missing value where TRUE/FALSE needed
states that for some i
, my_df$year[i] == df2$year[j]
is not TRUE
or FALSE
but rather NA
.声明对于某些
i
, my_df$year[i] == df2$year[j]
不是TRUE
或FALSE
而是NA
。 The reason most likely is that either my_df$year[i]
or df2$year[j]
is NA
for some i
or j
, respectively.最可能的原因是
my_df$year[i]
或df2$year[j]
对于某些i
或j
分别为NA
。 So I suggest that you check your my_df
for missing values.所以我建议你检查你的
my_df
是否有缺失值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.