Re: [問題] 依條件篩選資料&串檔

看板R_Language作者 (天)時間9年前 (2016/01/11 19:38), 9年前編輯推噓0(000)
留言0則, 0人參與, 最新討論串2/2 (看更多)
※ 引述《developme223 (allen)》之銘言: : [軟體熟悉度]: : 入門(寫過其他程式,只是對語法不熟悉) : [問題敘述]: : 目前有兩組資料,一組是首次上市櫃公司,另一組是已上市櫃公司 : ,想把已上市櫃公司對首次上市櫃公司做配對,配對條件為已上市櫃公司 : 的總資產介於[IPOs總資產*70%,IPOs總資產*130%]區間內,即符合配對條件。 : 資料型態如下: : 首次上市櫃公司(IPOs) 已上市櫃公司 : Code Asset Code Asset : 1 9955 2097959 1 1101 32922000 : 2 3535 2364161 2 1102 28988000 : 3 3519 13352343 3 1103 7258000 : 4 1583 6534372 4 . . : 5 4306 8500026 5 . . : . . . . . : . . . . . : . . . . . : . . . . . : 50 . . 1000 . . : ----> 想把整理資料成如下型式 : 首次上市櫃公司(IPOs) : IPOs$Code 已上市櫃公司$Code EX:9955的總資產*7/10=1468571.3 : 1 9955 1110 9955的總資產*13/10=2727346.7 : 1 9955 1201 總資產介於[1468571.3,2727346.7]的 : 1 9955 1203 已上市櫃公司,整理於IPOs$Code旁邊 : 1 9955 1216 。 : 1 9955 1218 : 2 3535 1503 : 2 3535 1507 : 2 3535 1530 : 2 3535 1537 : 2 3535 1525 : 3 3519 1618 : 3 3519 1537 : . . . : . . . : . . . : . . . : . . . : . . . : . . . : . . . : . . . : . . . : 50 . . : 50 . . : 50 . . : 想請教各位高手,in R 如何處理,感謝。 library(data.table) library(plyr) library(dplyr) library(magrittr) # data generation set.seed(15) # for reproduction numIPOs = 50 IPOs_DT = data.table(code = sprintf('%04i', sample(1:9999, numIPOs)), asset = rgamma(numIPOs, 20, 37)*10**sample(4:5, numIPOs, TRUE)) %>% tbl_dt(FALSE) # Source: local data table [50 x 2] # # code asset # (chr) (dbl) # 1 6021 57969.857 # 2 1951 61825.953 # 3 9662 44892.029 # 4 6507 6215.292 # 5 3669 4576.075 # 6 9883 5275.881 # 7 8147 7143.431 # 8 2538 6206.857 # 9 6867 6486.436 # 10 8306 58442.176 # .. ... ... # 我不知道已上市櫃公司怎麼簡寫,我就用publicly traded company (PTC) numPTCs = 1000 PTCs_DT = data.table(code = sample(setdiff(sprintf('%04i', 1:9999), IPOs_DT$code), numPTCs), asset = rgamma(numPTCs, 20, 37)*10**sample(4:5, numPTCs, TRUE)) %>% tbl_dt(FALSE) # Source: local data table [1,000 x 2] # # code asset # (chr) (dbl) # 1 9806 6434.788 # 2 6575 74880.446 # 3 5375 65993.185 # 4 6302 4111.247 # 5 3780 5615.484 # 6 7290 51964.095 # 7 7131 38961.114 # 8 0656 32934.782 # 9 9688 6426.658 # 10 3223 2596.685 # .. ... ... IPOs_DT %>% mutate(asset_p70 = asset*0.7, asset_p130 = asset*1.3) %>% ddply(.(code), function(subDT){ PTCs_DT %>% filter(asset >= subDT$asset_p70, asset <= subDT$asset_p130) %>% setnames(c('PTCs_code', 'PTCs_asset')) %>% mutate(IPOs_code = subDT$code, IPOs_asset = subDT$asset) }) %>% tbl_df %>% select(-code) # Source: local data frame [17,878 x 4] # # PTCs_code PTCs_asset IPOs_code IPOs_asset # (chr) (dbl) (chr) (dbl) # 1 6575 74880.45 0109 61598.19 # 2 9688 64266.58 0109 61598.19 # 3 4209 44089.71 0109 61598.19 # 4 8604 45265.31 0109 61598.19 # 5 4742 48381.22 0109 61598.19 # 6 6318 49848.99 0109 61598.19 # 7 6651 60117.82 0109 61598.19 # 8 4046 43568.23 0109 61598.19 # 9 6646 49588.79 0109 61598.19 # 10 2829 45721.72 0109 61598.19 # .. ... ... ... ... -- R資料整理套件系列文: magrittr #1LhSWhpH (R_Language) http://tinyurl.com/1LhSWhpH data.table #1LhW7Tvj (R_Language) http://tinyurl.com/1LhW7Tvj dplyr(上) #1LhpJCfB (R_Language) http://tinyurl.com/1LhpJCfB dplyr(下) #1Lhw8b-s (R_Language) tidyr #1Liqls1R (R_Language) http://tinyurl.com/1Liqls1R -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 140.109.73.231 ※ 文章網址: https://www.ptt.cc/bbs/R_Language/M.1452512281.A.141.html ※ 編輯: celestialgod (140.109.73.231), 01/11/2016 20:06:55
文章代碼(AID): #1MavGP51 (R_Language)
討論串 (同標題文章)
文章代碼(AID): #1MavGP51 (R_Language)