如何使用dplyr的setdiff将数据分成两组

时间:2015-02-07 21:42:04

标签: r

我正在使用dplyr将一些数据简单地拆分为训练和测试。

当我做一个简单的例子时,效果很好:

a = c(1, 2, 3, 4, 5, 6, 7, 8)
b = c("A", "B", "C", "D", "E", "F", "G", "H")

df = data.frame(a, b)

train = sample_frac(df, 0.8)
test = setdiff(df, train)

> nrow(train) + nrow(test) == nrow(df)
[1] TRUE

然而,当我尝试使用经典的UCI Wine数据集做同样的事情时,我似乎没有得到相同的结果:

wine = read.csv("http://www.nd.edu/~mclark19/learn/data/goodwine.csv")

wine_train = sample_frac(wine, 0.8)
wine_test = setdiff(wine, wine_train)

> nrow(wine_train) + nrow(wine_test) == nrow(wine)
[1] FALSE
> nrow(wine_train) + nrow(wine_test)
[1] 6105
> nrow(wine)
[1] 6497

我是否遗漏了setdiff的行为?

谢谢, AG

1 个答案:

答案 0 :(得分:7)

也许是因为有重复的行:

>any(duplicated(wine))
[1] TRUE

如果清理数据集:

drunk = wine[!duplicated(wine),]
drunk_train = sample_frac(drunk, 0.8)
drunk_test = setdiff(drunk, drunk_train)
nrow(drunk_test) + nrow(drunk_train) == nrow(drunk)
[1] TRUE