简体   繁体   English

如何通过R中的多个因素将函数应用于矩阵列?

[英]How to apply function over columns of matrix by multiple factors in R?

this is a simple question, and I am sure it is easily solvable with either tapply, apply, or by, etc. However, I am still relatively new to this, and I would like to ask for advice. 这是一个简单的问题,我相信可以通过轻按,应用或按以下方式轻松解决。但是,我对此还比较陌生,我想请教一下。

The problem: 问题:

I have a data frame with say 5 columns. 我有一个包含5列的数据框。 Columns 4 and 5 are factors, say. 例如,第4列和第5列是因素。 For each factor in column 5, I want to execute a function over columns 1:3 for each group in my column 5. This is, in principle, easily doable. 对于第5列中的每个因子,我想对第5列中的每个组的第1:3列执行一个函数。从原理上讲,这很容易实现。 However, I want to have the output as a nice table, and I want to learn how to do this in an elegant way, which is why I would like to ask you here. 但是,我想将输出作为一个漂亮的表,并且我想学习如何以一种优雅的方式进行操作,这就是为什么我想在这里问你。

Example: 例:

 df <- data.frame(x1=1:6, x2=12:17, x3=3:8, y=1:2, f=1:3)

Now, the command 现在,命令

 by(df[,1:3], df$y, sum)

would give me the sum based on each factor level in y , which is almost what I want. 会根据y每个因子水平给我总和,这几乎是我想要的。 Two additional steps are needed: one is to do this for each factor level in f . 需要执行两个附加步骤:一个是针对f每个因子水平执行此操作。 This is almost trivial. 这几乎是微不足道的。 I could easily wrap lapply around the above command and I would get what I want, except this: I want to generate a table with the results, and maybe even use it to generate a heatmap. 我可以轻松地在上面的命令周围包裹lapply ,得到我想要的东西,除了:我想用结果生成一个表,甚至可以用它来生成热图。

Hence: is there an easy and more elegant way to do this and to generate a matrix with corresponding output? 因此:有没有一种简单且更优雅的方法来执行此操作并生成具有相应输出的矩阵? This seems like an everyday-task for data scientists, which is why I suspect that there is an existing built-in solution... 对于数据科学家来说,这似乎是一项日常任务,这就是为什么我怀疑存在现有的内置解决方案的原因...

Thanks for any help or any hint, no matter how small! 感谢您的帮助或提示,无论大小如何!

You can use the reshape2 and plyr packages to accomplish this. 您可以使用reshape2plyr软件包来完成此操作。

library(plyr)
df2 <- ddply(df, .(y, f), sum)

and then to turn it into af by y matrix: 然后将其通过y矩阵转换为af:

library(reshape2)
acast(df2, f ~ y, value.var = "V1")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM