r - 两个相同的操作产生不同结构的输出
问题描述
我在编写一些代码时遇到了困难。以前代码可以正常工作,但现在不行。
为了突出这个问题,我提供了两个数据框。
我有两个数据框,dat 和 cleansvm。当我以相同的方式对两个帧进行子集时,我得到以下信息:
head(dat[-train,])
x.1 x.2 y
2 2.183643 3.6888733 1
3 1.164371 3.5865884 1
4 3.595281 1.6690922 1
5 2.329508 -0.2852355 1
6 1.179532 4.4976616 1
10 1.694612 2.5101084 1
head(dat[-train,"y"])
1 1 1 1 1 1
class(dat)
[1] "data.frame"
head(cleansvm[-train,])
Interpretation col1 col2
1: R 0 0
2: R 0 0
3: R 0 0
4: R 0 0
5: R 0 0
6: R 0 0
head(cleansvm[-train,"Interpretation"])
Interpretation
1: R
2: R
3: R
4: R
5: R
6: R
class(cleansvm)
[1] "data.table" "data.frame"
我的问题如下:为什么 和 的形式head(dat[-train,"y"])
不同head(cleansvm[-train,"Interpretation"])
,我可以做任何事情来获得head(cleansvm[-train,"Interpretation"])
与 相同的形式head(dat[-train,"y"])
吗?
head(dat[-train,"y"])
可以稍后在我的代码中用于形成表格,而head(cleansvm[-train,"Interpretation"])
由于它的长度不能。
我很确定 cleansvm 类是导致问题的原因,但我不知道为什么。我尝试将其转换为一个直接的数据框(它目前是一个数据表),但这并没有帮助。我也尝试过使用 t() ,head(cleansvm[-train,"Interpretation"])
但后来导致错误。
任何帮助表示赞赏。
解决方案
cleansvm
是一个data.table
对象。要获得预期的输出,您需要做
head(cleansvm[-train, Interpretation])
或者
head(cleansvm[-train,"Interpretation", with = F])
推荐阅读
- firebase - 是否有任何选项可以根据用户获取 Firestore 读取使用情况
- reactjs - 反应:当元素为真时无法显示按钮
- msbuild - MSBuild 获取测试的程序集名称
- javascript - 如何将html转换为draftjs?
- javascript - discord.js - 立即删除新添加的反应
- java - 如何重复每个字符重复次数减少的字符串?
- c# - 在 C# 代码中使用 httprequest 下载文件
- reactjs - 不推荐使用 React super(props) 吗?
- c++ - 将值插入排序数组而不破坏顺序的类函数
- python - df.combine() 是如何工作的?