如何从R数据帧的两列中共同采样?

时间:2019-10-28 22:23:45

标签: r shuffle sample

我有一个包含4列的数据框。我正在尝试将数据帧的两列一起改组,以使这两列始终相关。

我尝试了“采样”功能,但它仅限于数据帧的一列。


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))

2 个答案:

答案 0 :(得分:0)

对行进行采样,例如,如果要对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

答案 1 :(得分:0)

非常感谢您的建议。终于我找到了解决方案。代码如下。 我相信代码仍然可以优化。


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