我将数据集作为子集,这将导致数据帧具有非整数行名。请问这种现象背后的原因?
library(outbreaks)
df <- measles_hagelloch_1861[, 3, drop = FALSE]
df$disease <- 1
index <- sample(1:50, 50, replace = TRUE, prob = NULL)
syn_df <- df[index, ]
结果是
当您进行替换采样时,您将得到重复的行名(同一行采样不止一次)。行名必须是唯一的,因此添加了.1
以使它们唯一。
一个简单的例子,重复iris
数据集的第一行。
> iris[1, ]
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
> iris[c(1, 1), ]
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
1.1 5.1 3.5 1.4 0.2 setosa
> iris[c(1, 1, 1),]
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
1.1 5.1 3.5 1.4 0.2 setosa
1.2 5.1 3.5 1.4 0.2 setosa