得到在R A data.frame的样品(getting a sample of a data.fr

2019-06-27 03:58发布

我在R中的以下数据帧:

id<-c(1,2,3,4,10,2,4,5,6,8,2,1,5,7,7)
date<-c(19970807,19970902,19971010,19970715,19991212,19961212,19980909,19990910,19980707,19991111,19970203,19990302,19970605,19990808,19990706)
spent<-c(1997,19,199,134,654,37,876,890,873,234,643,567,23,25,576)
df<-data.frame(id,date,spent)

我需要3个客户(基于ID)的随机样本中提取的客户的所有意见的方式。

Answer 1:

你想用%in%unique

df[df$id %in% sample(unique(df$id),3),]
##    id     date spent
## 4   4 19970715   134
## 7   4 19980909   876
## 8   5 19990910   890
## 10  8 19991111   234
## 13  5 19970605    23

使用data.table避免$引用

library(data.table)
DT <- data.table(df)

 DT[id %in% sample(unique(id),3)]
##    id     date spent
## 1:  1 19970807  1997
## 2:  4 19970715   134
## 3:  4 19980909   876
## 4:  1 19990302   567
## 5:  7 19990808    25
## 6:  7 19990706   576

这可确保您始终评估data.table内的表达式。



Answer 2:

使用这样的:

df[sample(df$id, 3), ]
#   id     date spent
# 1  1 19970807  1997
# 5 10 19991212   654
# 8  5 19990910   890

当然,你的样品会有所不同。

更新

如果你想要独一无二的客户 ,您可以aggregate第一。

df2 = aggregate(list(date = df$date, spent = df$spent), list(id = df$id), c)
df2[sample(df2$id, 3), ]
#   id               date    spent
# 4  4 19970715, 19980909 134, 876
# 5  5 19990910, 19970605  890, 23
# 8  8           19991111      234

或者-用了一个选项aggregate

df[df$id %in% sample(unique(df$id), 3), ]
#    id     date spent
# 1   1 19970807  1997
# 3   3 19971010   199
# 12  1 19990302   567
# 14  7 19990808    25
# 15  7 19990706   576


文章来源: getting a sample of a data.frame in R