由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Statistics版 - generating percentile-percentage charts (转载)
相关主题
Generate and Retrieve Many Objects with Sequential NamesR:matrix
求助:data manipulation的一个问题【包子】从excel读取数据到R的问题
help in Rrbind或者cbind时候,vector的长度不一样怎么办呢?求助
R memory urgent help用R出现怪问题。
R question about apply() and outer()R help
再一个R 问题SAS 中 proc iml 怎么生成data file?
请教一个关于R的问题关于处理两个csv文件交集和子集的问题
【R】保留matrix中某些值[SQL] just figured out how to loop through all columns in a table
相关话题的讨论汇总
话题: age话题: visits话题: patient话题: clinic
进入Statistics版参与讨论
1 (共1页)
c***z
发帖数: 6348
1
【 以下文字转载自 DataSciences 讨论区 】
发信人: chaoz (晨钟暮鼓), 信区: DataSciences
标 题: generating percentile-percentage charts
发信站: BBS 未名空间站 (Mon Nov 24 20:11:11 2014, 美东)
Spent some time generating this kind of charts from raw data. There might be
better ways of doing so, but I would just post my method and 抛砖引玉。
Raw table has three columns: clinic | age | count, which records the age of
patients, rather, how many of each age category.
Target table has three columns: clinic | age_percentile | count_percentage,
which records the percentage of patients in each age category, with the
categories in percentiles form (e.g. if there are only two age categories,
then the percentiles would be 50 and 100).
Here is the R code (I knew Scala code must be simpler but my company is not
using it)
# order by clinic and age
visits <- visits[with(visits,
order(clinic, age)), ]
# percentiles of age
percentiles <- by(visits$age,
list(visits$clinic),
function(x) trunc(rank(x)/length(x) * 100),
simplify = T)
# percentages of count
percentages <- by(visits$count,
list(visits$clinic),
function(x) x / sum(x),
simplify = T)
# put them together
patient_percentiles <- cbind(row.names(percentiles),
percentiles,
percentages)

patient_percentiles <- data.frame(patient_percentiles)

# unpack list elements
patient_percentiles <- with(patient_percentiles,
cbind(melt(percentiles),
melt(percentages)))
# clean up
patient_percentiles <- patient_percentiles[, c(2,1,3)]
colnames(patient_percentiles) <- c("clinic", "age_percentiles", "count_
percentages")
1 (共1页)
进入Statistics版参与讨论
相关主题
[SQL] just figured out how to loop through all columns in a tableR question about apply() and outer()
求助 R density plot 出现波纹。。。再一个R 问题
How to add label in Excel bar chart请教一个关于R的问题
R package: how to use xx.rd files to generate PDF document?【R】保留matrix中某些值
Generate and Retrieve Many Objects with Sequential NamesR:matrix
求助:data manipulation的一个问题【包子】从excel读取数据到R的问题
help in Rrbind或者cbind时候,vector的长度不一样怎么办呢?求助
R memory urgent help用R出现怪问题。
相关话题的讨论汇总
话题: age话题: visits话题: patient话题: clinic