[英]How to take sample from two columns of R data frame jointly?
我有一個 4 列的數據框。 我正在嘗試將數據框的兩列混在一起,以使這兩列始終相關。
我試過“樣本”function,但它僅限於一列數據幀。
data = data.frame(label=letters[1:5], label2=letters[1:15], number=11:15)
data = within(data, numbersq <- (number*number))
# lable lable2 number numbersq
# a a 11 121
# b b 12 144
# c c 13 169
# d d 14 196
# e e 15 225
#Now, I want to twick the data something like, columns 'lable' and 'lable2' remains as it is and columns 'number' and 'numbersq' should shufffle.
#As you can see in the desired output,'number' and 'numbersq' should shuffled together not separately.
#Desired Output
# lable lable2 number numbersq
# a a 15 225
# b b 13 169
# c c 14 196
# d d 12 144
# e e 11 121
I have tried he following code but seems it shuffles the columns separately.
data_2 = data.frame(data_2$label, data_2$label2, sample(data_2$number), sample(data_2$numbersq))
取行的樣本,例如,如果您想要 5 行的樣本
set.seed(1)
row_sample <- sample(1:nrow(data),5)
data[row_sample,]
# label lable2 number numbersq
#7 g g 17 289
#2 b b 12 144
#3 c c 13 169
#8 h h 18 324
#1 a a 11 121
非常感謝您的建議。 最后我得到了解決方案。 代碼如下。 我相信代碼仍然可以優化。
data <- data.frame(label=letters[1:5], lable2=letters[1:5], number=11:15)
data = within(data, numbersq <- (number*number))
print(data)
# lable lable2 number numbersq
# a a 11 121
# b b 12 144
# c c 13 169
# d d 14 196
# e e 15 225
data_2a = data[,1:2]
data_2b = data[,3:4]
data_2b_samp = data_2b[sample(nrow(data_2b)), ]
data_3 = cbind(data_2a, data_2b_samp)
print(data_3)
# lable lable2 number numbersq
# a a 15 225
# b b 13 169
# c c 14 196
# d d 12 144
# e e 11 121
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.