我有兩個數據幀,我希望將其作爲一個標籤列添加到一個數據幀中;但rbind不能按預期工作,可能是因爲數據的因素:R當數據是因素時附加兩個數據幀
> str(trainLabels)
Factor w/ 2 levels "0","1": 2 1 1 2 1 2 1 2 2 1 ...
> head(trainLabels)
[1] 1 0 0 1 0 1
Levels: 0 1
> str(testLabels)
Factor w/ 2 levels "0","1": 2 1 2 1 1 1 1 2 1 1 ...
> head(testLabels)
[1] 1 0 1 0 0 0
Levels: 0 1
trainPlusTestLabels <- rbind(trainLabels, testLabels)
然後:
head(trainPlusTestLabels)
給了我一個奇怪的輸出。 trainPlusTestLabels沒有我想要的結構。
> str(trainPlusTestLabels)
int [1:2, 1:9000] 2 2 1 1 1 2 2 1 1 1 ...
- attr(*, "dimnames")=List of 2
..$ : chr [1:2] "trainLabels" "testLabels"
..$ : NULL
如何追加兩組標籤只有一列標籤?
它看起來* *我喜歡 「trainLabels」 和 「testLabels」 是'vector's,不'數據.frame's。在這種情況下,'rbind'會做出完全不同的事情。 – A5C1D2H2I1M1N2O1R2T1
你會想用'c()'來組合2個向量。 – tcash21
...或者'data.frame(train = trainLabels,test = testLabels)'如果你想讓它們作爲數據框中的列。 – joran