gpt4 book ai didi

r - 是否正式支持使用 [-0,] 删除 R 数据框中的所有行?

转载 作者:行者123 更新时间:2023-12-03 21:51:49 26 4
gpt4 key购买 nike

我发现我可以通过使用方括号函数进行子集化并使用 -0 作为行索引来删除 R 数据框的所有行。但是,我找不到任何文件说明这是我可以指望将来出现的官方行为。这是我可以放心使用的官方功能吗?希望它在未来的版本中继续以这种方式运行?

> df <- data.frame(c1=c(1,2,3),c2=c(2,3,4), c3=c(4,5,6))
> df
c1 c2 c3
1 1 2 4
2 2 3 5
3 3 4 6
> df[-0,]
[1] c1 c2 c3
<0 rows> (or 0-length row.names)

最佳答案

让我们检查文档。

help("[.data.frame") 说(强调我的):

When [ and [[ are used with two indices (x[i, j] and x[[i, j]]) they act like indexing a matrix [...]. Note that for each selected column, xj say, typically (if it is not matrix-like), the resulting column will be xj[i], and hence rely on the corresponding [ method, see the examples section.

下一站是 help("["),它没有说明如何使用 0 作为索引,而是指向“R 语言定义”手册。

There我们阅读(强调我的):

A special case is the zero index, which has null effects: x[0] is an empty vector and otherwise including zeros among positive or negative indices has the same effect as if they were omitted.

总结:

由于记录了此行为,您可以依靠 DF[0,] 返回具有空列向量的 data.frame。

关于r - 是否正式支持使用 [-0,] 删除 R 数据框中的所有行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30240921/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com