作者tony1331 (BLUE)
看板R_Language
標題[問題] 資料篩選(日期問題)
時間Thu Sep 21 15:39:13 2017
[問題類型]:
(我想用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://webptt.com/m.aspx?n=bbs/R_Language/M.1505979556.A.BE3.html
1F:推 cd12631: 把日期和時間切開各一欄 然後篩選時間 09/21 16:18
2F:推 s3714443: 用lubridate看看? 09/21 18:31
3F:→ clansoda: 用data.table最快 09/21 20:33
4F:→ clansoda: dt[between(hour(time), 0, 3, incbounds = FALSE)] 09/21 20:35
5F:→ clansoda: 我直接寫在PTT上的,有錯的話應該是括號逗號之類的錯了 09/21 20:35
6F:→ clansoda: 喔 上面那個3要改成2 09/21 20:35
8F:→ clansoda: 你有日期的type嗎? 我其實不太熟R的datetype 09/21 23:26
9F:→ clansoda: 你的是字串? 09/21 23:26
10F:推 clansoda: 因為hour要POXITlt才能跑,他轉不過去我不知道你的 09/21 23:33
11F:→ clansoda: datatype 你可能要class看看 09/21 23:33
12F:推 tan800630: 看字串形式的話 直接as.POSIXct(x) 試試? 09/21 23:35
13F:→ celestialgod: 直接regular expression取00:xx:xx或是01:xx:xx的格 09/22 08:39
14F:→ celestialgod: 式就好了,沒幾行 09/22 08:39
15F:→ celestialgod: dat[grepl('0[01]:\\d{2}:\\d{2}',dat$time),] 09/22 08:41
16F:→ obarisk: package lubridate 09/22 12:35
18F:→ obarisk: nettes/lubridate.html 09/22 12:35
19F:→ obarisk: dat %>% mutate(hr=hour(ymd_hms(datecol))) %>% filter( 09/22 12:37
20F:→ obarisk: hr==1) 09/22 12:37
21F:→ celestialgod: 本身就是字串還是回歸字串處理吧XDD,感覺原po也不 09/22 18:44
22F:→ celestialgod: 熟轉型 09/22 18:44
23F:→ clansoda: 重點是他人不見了吧,只要把字串轉完剩下的都差不多 09/22 19:03
24F:→ tony1331: 不好意思哦,有點忙,我是已經轉成時間了,我再試看看, 09/22 19:18
25F:→ tony1331: 謝謝大家。 09/22 19:18
26F:→ clansoda: 你只要格式正確的話上面幾個作法應該都可行 09/22 19:23
27F:→ clansoda: loop最好作為最後的牌,沒牌再打 09/22 19:23
29F:→ clansoda: 你的data是不是data.frame? 09/22 20:19
30F:→ clansoda: 我提的方法是基築於data.table package 09/22 20:19
31F:→ clansoda: 如果你不想用或不會用data.table 09/22 20:20
32F:→ clansoda: 可以參考cele大的或oba大的方法 cele大的方法不用 09/22 20:20
33F:→ clansoda: 額外的library 09/22 20:20
34F:→ tony1331: cel大,我跑你的code,他是篩出00:00:00~02:00:00,有詳 09/22 20:21
35F:→ tony1331: 細的說明嗎,謝謝~ 09/22 20:21
36F:→ tony1331: cl大,不好意思,我是不會用,謝謝你! 09/22 20:22
37F:→ celestialgod: 請google regular expression,其實只是這個的應用 09/22 20:25
38F:→ celestialgod: 而已 09/22 20:25
39F:→ tony1331: 好的,我馬上來google,感謝你!! 09/22 20:27
40F:推 clansoda: regex我還是常常要在那邊試來試去的 沒辦法一次到位 09/22 20:30
41F:→ celestialgod: 應該是不夠熟XDD 09/22 20:41
42F:→ celestialgod: 利用regexOne把題目都做完看看 09/22 20:41
43F:→ celestialgod: 其實多數會用到的regexp豆不難.... 09/22 20:42
44F:→ clansoda: 真的不常用,每次要用都要查 09/22 20:43
45F:→ celestialgod: 其實適用場域非常多,只是你要不要用而已,工作上, 09/22 20:46
46F:→ celestialgod: 幾乎每次程式都會用到,包刮oracle sql也會用 09/22 20:46