简体   繁体   English

如何拆分一列data.frame并获取data.frame作为输出?

[英]How to split one-column data.frame and get data.frame as output?

I used the code from this answer to split my train data into two sets. 我使用此答案中的代码将火车数据分为两组。

trainLabels <- read.csv(trainLabels.file, stringsAsFactors=F, header=FALSE)

> str(trainLabels)
'data.frame':   1000 obs. of  1 variable:
 $ V1: int  1 0 0 1 0 1 0 1 1 0 ...

trainLabelsTrain <- trainLabels[train_ind, ]
trainLabelsTest <- trainLabels[-train_ind, ]

> str(trainLabelsTrain)
 int [1:750] 0 1 0 0 0 0 1 1 1 0 ...

Then I would like to have a data.frame just like the original data ( trainLabels ). 然后,我想像原始数据( trainLabels )一样拥有一个data.frame。

How can I get a data.frame? 如何获得data.frame?

use the drop = FALSE command in your subsetting... 在子设置中使用drop = FALSE命令...

# drop = TRUE by default in `[` subsetting...
df <- data.frame( a = 1:10 )
df[ c(1,3,5) , ]
#[1] 1 3 5

#  With drop = FALSE...
df[ c(1,3,5) , , drop = FALSE ]
#  a
#1 1
#3 3
#5 5

When drop = TRUE R will attempt to coerce the result to the lowest possible dimension, in this case an atomic vector, as there is only a single column. drop = TRUE R将尝试将结果强制为最小尺寸,在这种情况下为原子矢量,因为只有一列。

Obviously I like @SimonO101's answer, but I just thought I'd add that one could also use the split function here: 显然,我喜欢@ SimonO101的答案,但我只是想补充一下,这里也可以使用split函数:

df <- data.frame(a = 1:10)
set.seed(1)
x <- rbinom(10,1,.5)
out <- split(df,x)

The result would be a list of two dataframes: 结果将是两个数据帧的列表:

> str(out)
List of 2
 $ 0:'data.frame':      4 obs. of  1 variable:
  ..$ a: int [1:4] 1 2 5 10
 $ 1:'data.frame':      6 obs. of  1 variable:
  ..$ a: int [1:6] 3 4 6 7 8 9

This is because drop=TRUE is the default in [ but drop=FALSE is the default in split . 这是因为drop=TRUE[的默认值,而drop=FALSEsplit的默认值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM