作者celestialgod (天)
看板R_Language
标题Re: [问题] 依条件筛选资料&串档
时间Mon Jan 11 19:37:57 2016
※ 引述《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/cn.aspx?n=bbs/R_Language/M.1452512281.A.141.html
※ 编辑: celestialgod (140.109.73.231), 01/11/2016 20:06:55