簡體   English   中英

Using the "sample" function in r to randomly select rows from aa data frame into a new dataframe, while also storing non-sampled rows

[英]Using the "sample" function in r to randomly select rows from a a data frame into a new dataframe, while also storing non-sampled rows

我的學校項目的目標是將 select 數據集的一部分隨機放入一個新的子集,同時還使用基礎 ZE1E1D3D40573127E9ZEE048 中的“樣本”function 將非抽樣觀察結果存儲在另一個數據框中。

使用下面的代碼會產生我組成的數據框的隨機樣本。

DATA <- data.frame(x=c(3, 5, 6, 6, 8, 12, 14),
                 y=c(12, 6, 4, 23, 25, 8, 9),
                  z=c(2, 7, 8, 8, 15, 17, 29))



sample <- DATA[sample(1:nrow(DATA), floor(nrow(DATA)*0.7), replace = FALSE),]

但是,當我也想提取非抽樣觀察時遇到了麻煩,這就是我遇到麻煩的地方。 我遇到的大多數資源都建議類似於下面的代碼,

training <- DATA[sample,]
testing <- DATA[-sample,]

但該選項會產生錯誤消息

xj[i] 中的錯誤:無效的下標類型“列表”

任何幫助解決這種情況將不勝感激。

在您的代碼中, sample是 dataframe。 您需要sample作為索引才能使DATA[sample,]DATA[-sample,]工作。

sample <- sample(nrow(DATA), floor(nrow(DATA)*0.7))

training <- DATA[sample,]
testing <- DATA[-sample,]

簡化了sample(..)調用。

  • sample(1:nrow(DATA), ..)sample(nrow(DATA), ..)相同
  • 默認情況下, sample中的replaceFALSE ,因此無需明確提及。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM