-6

我有一个包含两列和不定行数的文件。第 1 行包含一个人员 ID,第 2 行包含每个人员 ID 的分数。我正在尝试随机打乱分数并将它们随机重新分配给新的 2 列数据框中的人员 ID。基本上破坏了分数和人名之间的真实关系。我怎样才能在 R 中做到这一点?我不知道如何编写一个函数来做到这一点,谢谢。

4

1 回答 1

3

As @Dason said in the comments it looks like you are just looking for sample. I have to say this is pretty basic stuff and even a rudimentary google query (or with your other favourite search engine) would have given you many posts and links on how to do this.

I assume that your data is in data.frame called data and it has a column called "id" for the person IDs and a column called "score" for the scores. You can easily make a new data.frame using sample like so...

data2 <- data.frame( ID = sample( data$id , nrow( data ) ) , Score = sample( data$score , nrow( data ) ) )
于 2013-03-29T21:56:42.820 回答