我对我写的一些代码有困难。以前代码工作正常,但现在不能正常工作。
为了突出这个问题,我提供了两个数据帧。
我有两个数据帧,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类是导致问题的原因,但我不知道为什么。我试着把它转换成直接的数据帧(它现在是一个数据表),但是没有用。我还尝试在head(cleansvm[-train,"Interpretation"])上使用t(),但后来导致了错误。
感谢您的帮助。
发布于 2019-10-21 22:19:25
cleansvm是一个data.table对象。要获得预期的输出,您需要这样做
head(cleansvm[-train, Interpretation])或
head(cleansvm[-train,"Interpretation", with = F])https://stackoverflow.com/questions/58488090
复制相似问题