dataframe [,-1]和dataframe [-1]是否相同? [英] Are dataframe[ ,-1] and dataframe[-1] the same?

查看:475
本文介绍了dataframe [,-1]和dataframe [-1]是否相同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,这似乎是一个非常愚蠢的问题,但是 dataframe [,-1] dataframe [-1] 相同,它适用于所有数据类型?



为什么他们是相同的

解决方案

几乎。



[ - 1] 使用数据的事实。 frame是一个列表,所以当你做 dataframe [-1] 它会返回另一个没有第一个元素(即列)的data.frame(list)。



[,-1] 使用data.frame是一个二维数组,所以当你做 dataframe [,-1] 你得到不包括第一列的子数组。



一个先验,他们听起来像相同,但第二种情况也默认尝试减少其返回的子阵列的维度。因此,根据数据框的尺寸,您可能会收到一个data.frame或一个向量,例如:

 >数据<  -  data.frame(a = 1:2,b = 3:4)
> class(data [-1])$ ​​b $ b [1]data.frame
> class(data [,-1])$ ​​b $ b [1]integer

使用 drop = FALSE 来覆盖该行为:

 > class(data [,-1,drop = FALSE])
[1]data.frame


Sorry this seems like a really silly question but are dataframe[ ,-1] and dataframe[-1] the same, and does it work for all data types?

And why are they the same

解决方案

Almost.

[-1] uses the fact that a data.frame is a list, so when you do dataframe[-1] it returns another data.frame (list) without the first element (i.e. column).

[ ,-1]uses the fact that a data.frame is a two dimensional array, so when you do dataframe[, -1] you get the sub-array that does not include the first column.

A priori, they sound like the same, but the second case also tries by default to reduce the dimension of the subarray it returns. So depending on the dimensions of your dataframe you may get a data.frame or a vector, see for example:

> data <- data.frame(a = 1:2, b = 3:4)
> class(data[-1])
[1] "data.frame"
> class(data[, -1])
[1] "integer"

You can use drop = FALSE to override that behavior:

> class(data[, -1, drop = FALSE])
[1] "data.frame"

这篇关于dataframe [,-1]和dataframe [-1]是否相同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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