看板 R_Language 關於我們 聯絡資訊
雖然「在這個日期之前和之後做[交割年月]的篩選」的描述看不懂是怎樣 的操作,但是應該是類似這樣的狀況吧? # 如果需要安裝 package 的話 uncomment # install.packages(c("lubridate", "timeDate")) library(lubridate) library(timeDate) library(dplyr) # 指定要找出「每月第三個星期三」的時間段 start_date <- "2007-09-01" end_date <- "2014-06-01" # 載入原始檔 # 注意如果日期格式不是 "YYYY-MM-DD" 的話後面的部分要相應修改 test <- read.table("test.txt", header = TRUE, stringsAsFactors = FALSE) # 找出指定時間段的每月第三個週三 # 用 format() 把 timeDate 轉成 character period <- timeSequence(from = start_date, to = end_date, by = "month") thirdwed <- format(timeNthNdayInMonth(period, nday = 3, nth = 3, format = "%Y-%m-%d")) # 只留下 Date 為每月第三個週三的 observation test_fil <- filter(test, Date %in% thirdwed) # 依據交割年月做篩選 # 這邊看不懂原 po 想做什麼篩選... result <- filter(test_fil, `交割年月` == 201101) ※ 引述《poker0531 (破殼)》之銘言: : [問題類型]: : 程式諮詢(我想用R 做某件事情,但是我不知道要怎麼用R 寫出來) : [軟體熟悉度]: : 新手(沒寫過程式,R 是我的第一次) : [問題敘述]: : 我想篩選一串日期中每月的第三個周三, 並在這個日期之前和之後做[交割年月]的篩選 : [程式範例]: : 資料長這樣: : Date Time 交割年月 成交時間 成交價格 成交數量(B+S) : 1 2011-01-03 084500 201101 8450000 9000 24 : 2 2011-01-03 084500 201101 8450000 9000 2 : 3 2011-01-03 084500 201101 8450000 9000 2 : 4 2011-01-03 084500 201101 8450000 9000 6 : 5 2011-01-03 084500 201101 8450000 9000 2 : 6 2011-01-03 084500 201101 8450000 9000 2 : 結算日的判斷方式有google到如下: : require(lubridate) : x<-day(unique(" : Con<-function(x,y){ : if(day(x)>=15 && day(x)<=21 && y==3){ : "SettleDay" : }else if ((day(x)>=16 && day(x)<=22 && y==4)|| : (day(x)>=17 && day(x)<=23 && y==5)|| : (day(x)>=18 && day(x)<=24 && y==6)|| : (day(x)>=20 && day(x)<=26 && y==1)|| : (day(x)>=21 && day(x)<=27 && y==1)|| : (day(x)>=22)){ : "SettleDay" : } : } : 感謝各位大神幫忙,搞了我兩個周末了 : [環境敘述]: : R version 3.5.1 (2018-07-02) : Platform: x86_64-w64-mingw32/x64 (64-bit) : Running under: Windows >= 8 x64 (build 9200) : Matrix products: default : locale: : [1] LC_COLLATE=Chinese (Traditional)_Taiwan.950 LC_CTYPE=Chinese : (Traditional)_Taiwan.950 LC_MONETARY=Chinese (Traditional)_Taiwan.950 : [4] LC_NUMERIC=C LC_TIME=Chinese : (Traditional)_Taiwan.950 : attached base packages: : [1] stats graphics grDevices utils datasets methods base : other attached packages: : [1] lubridate_1.7.4 : loaded via a namespace (and not attached): : [1] compiler_3.5.1 magrittr_1.5 tools_3.5.1 Rcpp_0.12.18 : stringi_1.2.4 stringr_1.3.1 : [關鍵字]: : 台指期結算日判斷 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 61.228.161.102 ※ 文章網址: https://www.ptt.cc/bbs/R_Language/M.1533571890.A.C61.html