R_Language 板


LINE

※ 引述《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://webptt.com/m.aspx?n=bbs/R_Language/M.1452512281.A.141.html ※ 編輯: celestialgod (140.109.73.231), 01/11/2016 20:06:55







like.gif 您可能會有興趣的文章
icon.png[問題/行為] 貓晚上進房間會不會有憋尿問題
icon.pngRe: [閒聊] 選了錯誤的女孩成為魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一張
icon.png[心得] EMS高領長版毛衣.墨小樓MC1002
icon.png[分享] 丹龍隔熱紙GE55+33+22
icon.png[問題] 清洗洗衣機
icon.png[尋物] 窗台下的空間
icon.png[閒聊] 双極の女神1 木魔爵
icon.png[售車] 新竹 1997 march 1297cc 白色 四門
icon.png[討論] 能從照片感受到攝影者心情嗎
icon.png[狂賀] 賀賀賀賀 賀!島村卯月!總選舉NO.1
icon.png[難過] 羨慕白皮膚的女生
icon.png閱讀文章
icon.png[黑特]
icon.png[問題] SBK S1安裝於安全帽位置
icon.png[分享] 舊woo100絕版開箱!!
icon.pngRe: [無言] 關於小包衛生紙
icon.png[開箱] E5-2683V3 RX480Strix 快睿C1 簡單測試
icon.png[心得] 蒼の海賊龍 地獄 執行者16PT
icon.png[售車] 1999年Virage iO 1.8EXi
icon.png[心得] 挑戰33 LV10 獅子座pt solo
icon.png[閒聊] 手把手教你不被桶之新手主購教學
icon.png[分享] Civic Type R 量產版官方照無預警流出
icon.png[售車] Golf 4 2.0 銀色 自排
icon.png[出售] Graco提籃汽座(有底座)2000元誠可議
icon.png[問題] 請問補牙材質掉了還能再補嗎?(台中半年內
icon.png[問題] 44th 單曲 生寫竟然都給重複的啊啊!
icon.png[心得] 華南紅卡/icash 核卡
icon.png[問題] 拔牙矯正這樣正常嗎
icon.png[贈送] 老莫高業 初業 102年版
icon.png[情報] 三大行動支付 本季掀戰火
icon.png[寶寶] 博客來Amos水蠟筆5/1特價五折
icon.pngRe: [心得] 新鮮人一些面試分享
icon.png[心得] 蒼の海賊龍 地獄 麒麟25PT
icon.pngRe: [閒聊] (君の名は。雷慎入) 君名二創漫畫翻譯
icon.pngRe: [閒聊] OGN中場影片:失蹤人口局 (英文字幕)
icon.png[問題] 台灣大哥大4G訊號差
icon.png[出售] [全國]全新千尋侘草LED燈, 水草

請輸入看板名稱,例如:Soft_Job站內搜尋

TOP