簡體   English   中英

R - group_by 條件的特定值

[英]R - group_by specific values for a condition

我想使用 group_by 函數隨機采樣具有多種條件的數據集:

output<-files %>% group_by(location, time) %>% sample_n(3)

但是,有沒有辦法在每個條件中指定要采樣的內容? 例如這樣的事情:

output<-files %>% group_by(location(c[1:2]), time(c[00:00:00-01:00:00])) %>% sample_n(3)

所以原始數據框:

Location    Time
1           00:00:00
1           00:02:22
1           00:04:12
1           00:30:00
1           01:00:00
1           01:27:00
1           02:00:00
1           03:00:00
1           03:31:00
2           00:00:00
2           00:03:33
2           00:04:44
2           01:00:00
2           02:00:00
2           03:00:00
3           00:00:00
3           01:00:00
3           02:00:00
3           03:00:00

可能看起來像這樣(為簡單起見,數據框有限):

Location    Time
1           00:00:00
1           00:02:22
1           01:00:00           
2           00:00:00
2           00:03:33
2           00:04:44 

也許這會有所幫助

library(chron)
library(dplyr)
df1 %>% 
    filter(times(Time)  >= times('00:00:00') & times(Time) <= times('01:00:00')) %>%
    #or use between
    #filter(between(times(Time), times('00:00:00'), times('01:00:00'))) %>%  
    group_by(Location) %>%
    filter(n() >=3) %>%
    sample_n(3)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM