【发布时间】:2015-07-12 14:40:23
【问题描述】:
这是我的数据框示例
charact_fraction pure_charact sample replicate identity
0.08348135 clean An006 1 70
0.078947368 clean An006 1 70
0.090277778 clean An006 1 70
0.044399596 clean An006 2 70
0 clean An006 2 70
0.049348869 clean An006 2 70
0.218818381 mixed An011 1 70
0.112068966 mixed An011 1 70
1 pure An011 1 70
0 clean An011 2 70
0.214285714 mixed An011 2 70
0.2180937 mixed An011 2 70
我想对charact_fraction 进行分类并计算按多个因素分组的分类频率。生成的数据框应该是这样的
bin_frequency bin sample replicate identity
… 0-0.1 An006 1 70
… … … … …
… 0.9-1.0 An006 1 70
… 0-0.1 An011 1 70
… … … … …
… 0.9-1.0 An011 1 70
… … … … …
我有返回 bin 频率的函数。
get_freqs <- function(dat_vector, breaks) {
hist(dat_vector, breaks=breaks, include.lowest=TRUE, plot=FALSE)$counts
}
我可以生成垃圾箱。
breaks=seq(0,1,by=0.1)
bins = paste(breaks, breaks[-1], sep="-")
bins = bins[-length(ranges)]
我相信这是我目前为止最接近的镜头,但显然与预期的输出相差甚远:
with(df, tapply(charact_part, list(sample, replicate, identity), get_freqs, breaks=breaks))
我有非常丑陋的 Python 代码来做这件事,但我想在 R 中拥有一些更清洁和实用的东西。提前谢谢你。
【问题讨论】:
-
我猜你正在寻找
cut虽然我不确定你想要的输出是否真的。