Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用带有if-else条件的for循环创建向量_R - Fatal编程技术网

使用带有if-else条件的for循环创建向量

使用带有if-else条件的for循环创建向量,r,R,如何将下面for循环的3项输出转换为数据帧。在尝试解决方案时,我尝试了: -创建与for循环相关的对象,但无法成功 -创建一个矩阵,没有效果 什么代码可以将输出转换为向量或列表 > for(i in X$Planned)ifelse(is.na(i),print("ISNA"),print("NOTNA")) [1] "NOTNA" [1] "NOTNA" [1] "ISNA" 你能提出你的问题并分享dput(X$planning)吗?为什么不X$IsNA我感谢你的支持r2evans,这

如何将下面for循环的3项输出转换为数据帧。在尝试解决方案时,我尝试了:

-创建与for循环相关的对象,但无法成功 -创建一个矩阵,没有效果

什么代码可以将输出转换为向量或列表

> for(i in X$Planned)ifelse(is.na(i),print("ISNA"),print("NOTNA"))
[1] "NOTNA"
[1] "NOTNA"
[1] "ISNA"

你能提出你的问题并分享
dput(X$planning)
吗?为什么不
X$IsNA我感谢你的支持r2evans,这解决了问题
sapply(x$Planned, function(elem) if (is.na(elem)) {"isNA"} else {"notNA"})

# this will do it!

# however, it will be slower than the vectorized form

ifelse(is.na(x$Planned), "isNA", "notNA")