相关疑难解决方法(0)

如何使用ddply或aggregate返回向量(如fivenum)的函数?

我想使用几列来分割我的数据框,并fivenum在每个组上调用let .

aggregate(Petal.Width ~ Species, iris, function(x) summary(fivenum(x)))
Run Code Online (Sandbox Code Playgroud)

返回的值是一个只有2列的data.frame,第二个是矩阵.如何将其转换为data.frame的普通列?

更新

我希望使用更少的代码,如下所示 fivenum

ddply(iris, .(Species), summarise,
      Min = min(Petal.Width),
      Q1 = quantile(Petal.Width, .25),
      Med = median(Petal.Width),
      Q3 = quantile(Petal.Width, .75),
      Max = max(Petal.Width)
      )
Run Code Online (Sandbox Code Playgroud)

aggregate r plyr

8
推荐指数
2
解决办法
2881
查看次数

在reshape2中使用timevar转换数据框,与重塑基函数一样?

假设我有以下数据框:

d <- data.frame(id=c(1,1,1,2,2,3,3,3), time=c(1,2,3,1,2,1,2,3), var=runif(8))

 d
  id time       var
1  1    1 0.3733586
2  1    2 0.5743769
3  1    3 0.8253280
4  2    1 0.8136957
5  2    2 0.8726963
6  3    1 0.1105549
7  3    2 0.9527002
8  3    3 0.5690021
Run Code Online (Sandbox Code Playgroud)

使用基本reshape功能,我可以通过指定a ìdvar(识别属于同一单元的行)和a timevar(识别同一单元的不同观察值)将其转换为"宽"格式:

reshape(d, idvar="id", timevar="time", direction="wide")

  id     var.1     var.2     var.3
1  1 0.3733586 0.5743769 0.8253280
4  2 0.8136957 0.8726963        NA
6  3 0.1105549 0.9527002 0.5690021
Run Code Online (Sandbox Code Playgroud)

我试图用它的dcast功能来做reshape2,但没找到方法.你知道是否有可能吗?

编辑: Ananda Mahto的评论和回答是完全正确的,真正的问题是当原始数据框有多个 …

r reshape2

5
推荐指数
1
解决办法
4602
查看次数

标签 统计

r ×2

aggregate ×1

plyr ×1

reshape2 ×1