R使用一个因子在数据框架上应用 [英] R Applying a formular using a factor over a data frame

查看:97
本文介绍了R使用一个因子在数据框架上应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一些帮助,如何运行公式的几个变量和基于因素,所以一个例子基于一小时数据一个星期:

  df<  -  data.frame(a = runif(168),
+ b = runif(168),
+ c = runif(168),
+ d = rep(1:7,each = 24))

attach(df)

fx< - function(x){mean(x)}#复杂,只是为了说明

a,b,c是不同的变量,d是我的因素。 p>

这里我有一个每一个因素,我想将它应用于所有3个变量,并将每个结果写入一个单独的向量/数据框。所以现在做的是:

  a.mean< -tapply(a,d,fx)
b.mean< -tapply(b,d,fx)
c.mean< -tapply(c,d,fx)

这让我很确定,我错过了一些整洁的技巧,但似乎找不到。我想要能够放弃因子变量,只是循环指定的时间段以及尽可能多的变量。

解决方案

使用公式界面 aggregate ,点()可用于表示所有其他变量:

 聚合(。〜d ,df,mean)
dabc
1 1 0.5444300 0.4348559 0.5543393
2 2 0.5997199 0.4751082 0.5116904
3 3 0.4195746 0.6696669 0.5239728
4 4 0.4764139 0.5102245 0.4901829
5 5 0.3938329 0.3792583 0.4826971
6 6 0.4633260 0.5518397 0.4558116
7 7 0.4814347 0.4946845 0.5371871

请注意,您不需要使用此参数附加data.frame(因为它提供给 aggregate 作为参数)。


I would like some help on how to run formulas over several variables and based on factors, so an example based on hourly data for a week:

df<- data.frame(a = runif(168),
+               b = runif(168),
+               c = runif(168),
+               d = rep(1:7, each = 24))

attach(df)

fx<-function(x) { mean(x) } # it can be more complicated, just to illustrate

a, b, c are different variables, d is my factor.

Here I have a made a factor for each day and I would like apply it over all 3 variables, and write each result into a seperate vector/dataframe. So, what im doing now is:

a.mean <-tapply(a, d, fx)
b.mean <-tapply(b, d, fx)
c.mean <-tapply(c, d, fx)

Which makes me pretty sure there are some neat tricks I've missed but can't seem to find. I would like to be able to drop the factor variable and just loop over specified periods and over as many variables as I would like to.

解决方案

Using formula interface to aggregate, the dot (.) can be used to represent all other variables:

aggregate(.~d, df, mean)
  d         a         b         c
1 1 0.5444300 0.4348559 0.5543393
2 2 0.5997199 0.4751082 0.5116904
3 3 0.4195746 0.6696669 0.5239728
4 4 0.4764139 0.5102245 0.4901829
5 5 0.3938329 0.3792583 0.4826971
6 6 0.4633260 0.5518397 0.4558116
7 7 0.4814347 0.4946845 0.5371871

Note that you don't need to attach the data.frame using this either (as it is supplied to aggregate as a parameter).

这篇关于R使用一个因子在数据框架上应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆