看板 R_Language 關於我們 聯絡資訊
[問題類型]: (我想用R 做某件事情,但是我不知道要怎麼用R 寫出來) [軟體熟悉度]: (沒寫過程式,R 是我的第一次)s [問題敘述]: http://i.imgur.com/woqwvM1.jpg Table有一欄是時間,假設是7-8月的資料, 請問如果我只想要1點到2點的資料該如何寫 (不管日期 只要1點到2點) 謝謝~ ----- Sent from JPTT on my Asus ASUS_Z012DA. -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 223.141.61.125 ※ 文章網址: https://www.ptt.cc/bbs/R_Language/M.1505979556.A.BE3.html
cd12631: 把日期和時間切開各一欄 然後篩選時間 09/21 16:18
s3714443: 用lubridate看看? 09/21 18:31
clansoda: 用data.table最快 09/21 20:33
clansoda: dt[between(hour(time), 0, 3, incbounds = FALSE)] 09/21 20:35
clansoda: 我直接寫在PTT上的,有錯的話應該是括號逗號之類的錯了 09/21 20:35
clansoda: 喔 上面那個3要改成2 09/21 20:35
tony1331: c大我跑出來結果這樣http://i.imgur.com/DwkkS4a.jpg 09/21 23:02
clansoda: 你有日期的type嗎? 我其實不太熟R的datetype 09/21 23:26
clansoda: 你的是字串? 09/21 23:26
clansoda: 因為hour要POXITlt才能跑,他轉不過去我不知道你的 09/21 23:33
clansoda: datatype 你可能要class看看 09/21 23:33
tan800630: 看字串形式的話 直接as.POSIXct(x) 試試? 09/21 23:35
celestialgod: 直接regular expression取00:xx:xx或是01:xx:xx的格 09/22 08:39
celestialgod: 式就好了,沒幾行 09/22 08:39
celestialgod: dat[grepl('0[01]:\\d{2}:\\d{2}',dat$time),] 09/22 08:41
obarisk: package lubridate 09/22 12:35
obarisk: nettes/lubridate.html 09/22 12:35
obarisk: dat %>% mutate(hr=hour(ymd_hms(datecol))) %>% filter( 09/22 12:37
obarisk: hr==1) 09/22 12:37
celestialgod: 本身就是字串還是回歸字串處理吧XDD,感覺原po也不 09/22 18:44
celestialgod: 熟轉型 09/22 18:44
clansoda: 重點是他人不見了吧,只要把字串轉完剩下的都差不多 09/22 19:03
tony1331: 不好意思哦,有點忙,我是已經轉成時間了,我再試看看, 09/22 19:18
tony1331: 謝謝大家。 09/22 19:18
clansoda: 你只要格式正確的話上面幾個作法應該都可行 09/22 19:23
clansoda: loop最好作為最後的牌,沒牌再打 09/22 19:23
tony1331: c大,跑出來還是有問題http://i.imgur.com/iUppeip.jpg 09/22 20:03
clansoda: 你的data是不是data.frame? 09/22 20:19
clansoda: 我提的方法是基築於data.table package 09/22 20:19
clansoda: 如果你不想用或不會用data.table 09/22 20:20
clansoda: 可以參考cele大的或oba大的方法 cele大的方法不用 09/22 20:20
clansoda: 額外的library 09/22 20:20
tony1331: cel大,我跑你的code,他是篩出00:00:00~02:00:00,有詳 09/22 20:21
tony1331: 細的說明嗎,謝謝~ 09/22 20:21
tony1331: cl大,不好意思,我是不會用,謝謝你! 09/22 20:22
celestialgod: 請google regular expression,其實只是這個的應用 09/22 20:25
celestialgod: 而已 09/22 20:25
tony1331: 好的,我馬上來google,感謝你!! 09/22 20:27
clansoda: regex我還是常常要在那邊試來試去的 沒辦法一次到位 09/22 20:30
celestialgod: 應該是不夠熟XDD 09/22 20:41
celestialgod: 利用regexOne把題目都做完看看 09/22 20:41
celestialgod: 其實多數會用到的regexp豆不難.... 09/22 20:42
clansoda: 真的不常用,每次要用都要查 09/22 20:43
celestialgod: 其實適用場域非常多,只是你要不要用而已,工作上, 09/22 20:46
celestialgod: 幾乎每次程式都會用到,包刮oracle sql也會用 09/22 20:46