1
我希望使用plyr使用ecdf()
计算多个经验累积分布函数,然后将这些函数适当地应用于数据框中的条目。例如:使用plyr应用存储在列表中的函数
# Use the diamonds dataset in ggplot2
library(diamonds)
library(plyr)
# Calculate an ecdf for each combination of cut and color
all_ecdfs <- dlply(diamonds, c("cut", "color"), function(x) ecdf(x$carat))
# Make a dataset of specific diamonds, which I want to compare to the larger set
# My particular subset of diamonds
my_diamonds <- ddply(diamonds, c("cut", "color"), summarise,
my.carat=runif(n=1, min=0.5, max=1))
如果我手动做到这一点,它会是这个样子:
# Use the ecdf for the first entry: cut=="Fair" and color=="D"
my_diamonds$percentile <- NA
my_diamonds$percentile[my_diamonds$cut=="Fair" & my_diamonds$color=="D"] <-
all_ecdfs[["Fair.D"]](my_diamonds$my.carat[my_diamonds$cut=="Fair" & my_diamonds$color=="D"])
好像应该有某种方式使用ldply
或lapply
自动做到这一点,但我无法弄清楚。
有趣的是'dplyr'可以使数据帧,那些名单列,但(据我可以告诉) 'plyr'不能。 –