简体   繁体   English

如何将随机或任意列附加到数据帧[R]

[英]How to append a random or arbitrary column to data frame [R]

Hear me out. 听我说。 Consider an arbitrary case where the new column's elements do not require any information from other columns (which I frustrates base $ and mutate assignment), and not every element in the new column is the same. 考虑一个任意情况,其中新列的元素不需要其他列的任何信息(我挫败了基础$和mutate分配),并且并非新列中的每个元素都是相同的。 Here is what I've tried: 这是我尝试过的:

df$rand<-rep(sample(1:100,1),nrow(df))
unique(df$rand)
[1] 58

and rest assured, nrow(df)>1 . 放心, nrow(df)>1 I think the correct solution might have to do with an apply function? 我认为正确的解决方案可能与apply函数有关?

Your code repeats one single random number nrow(df) times. 您的代码重复一个随机数nrow(df)次。 Try instead: 请尝试:

df$rand<-sample(1:100, nrow(df))

This samples without replacement from 1:100 nrow(df) times. 1:100 nrow(df)次开始,该样本无需更换 Now this would give you an error if nrow(df)>100 because you would run out of numbers from 1:100 to sample. 现在,如果nrow(df)>100会给您一个错误,因为您将用完从1:100到采样的数字。 To make sure you don't get this error, you can instead sample with replacement : 为了确保您不会出现此错误,您可以改为使用替换示例:

df$rand<-sample(1:100, nrow(df), replace = TRUE)

If, however, you don't want any random numbers to repeat but would also like to prevent the error, you can do something like this: 但是,如果您不希望重复任何随机数,但又想防止错误发生,则可以执行以下操作:

df$rand<-sample(1:nrow(df), nrow(df))

if I understand this correctly ,I think this is pretty easily doable in dplyr or data.table . 如果我正确理解这一点,我认为这在dplyr或data.table中很容易实现。

for eg dplyr soln on iris 用于虹膜上的dplyr soln

iris%>%mutate(sample(n()))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM