为什么空数据帧失败is.null()测试? [英] Why does an empty dataframe fail an is.null() test?

查看:254
本文介绍了为什么空数据帧失败is.null()测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请原谅我,如果我的问题是相当基本的。
我通过 df< - data.frame()创建一个空数据帧,显然数据帧为NULL(空)。当我尝试通过 is.null(df)检查数据框是否为空时,结果为FALSE。在R中有 NULL empty 之间有任何差异。在这种情况下,如果数据框不是 NULL ,那么什么是空数据框架,何时将是 NULL 。谢谢

解决方案

df 不是NULL,因为它是一个数据框架因此具有一些定义的属性。例如,它有一个类。并且您可以使用 nrow(df)获取数据框中的行数,即使结果应该为零。因此,行数也是明确定义的。



按照我所知,fas不存在 is.empty 命令在base R.你可以做的是,例如,以下

  is.data.frame(df)& ;& nrow(df)== 0 

这将为空数据框(即,一个没有行),否则为false。



首先检查 is.data.frame 的原因是 nrow 可能会导致错误,如果它应用于数据帧以外的其他任何内容。由于&& nrow(df)只有在 df 是一个数据框。


please excuse me if my question is quite basic. I created an empty data frame by df <- data.frame() and obviously the data frame is NULL (empty). when I try to check if the data frame is empty by is.null(df), the result comes FALSE. Is there any difference between NULL and empty in R. In this case if the data frame is not NULL , then what is in the empty data frame and when it will be NULL. Thanks

解决方案

df is not NULL because it is a data frame and thus has some defined properties. For instance, it has a class. And you can get the number of rows in the data frame using nrow(df), even if the result should happen to be zero. Therefore, also the number of rows is well-defined.

As fas as I know, there is no is.empty command in base R. What you could do is, e.g., the following

is.data.frame(df) && nrow(df)==0

This will give TRUE for an empty data frame (that is, one with no rows) and false otherwise.

The reason for checking is.data.frame first is that nrow might cause an error, if it is applied to anything else than a data frame. Thanks to &&, nrow(df) will only be evaluated if df is a data frame.

这篇关于为什么空数据帧失败is.null()测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆