Reputation: 2072
I am using dlply to apply a function to a data.frame and generate a list for each combination of factors:
first I generate some data
is1 <- data.frame()
for( s in c("Big","Med", "Sma")) {
for(i in 1:3){
c <- rnorm(1,0,10)
n <- rnorm(1,-1,10)
is1 <- rbind(is1,data.frame(Site=i,SizeClass=s,d13C=rnorm(10,c,2),d15N=rnorm(10,n,2)))
}
}
them apply the function from siar package
require(plyr)
require(siar)
ell <-dlply(is1,.(Site,SizeClass),function(x) standard.ellipse(x$d13C,x$d15N,steps=1))
I want a data.frame with some fields so I try to use ldply with a function
extract_SEAc <- function(e)
{
c(sapply(e, '[[', 'xSEAc'),sapply(e, '[[', 'ySEAc'))
}
ell <- ldply(ell, .(Site,SizeClass), extract_SEAc )
but it gives an error
error: trying to apply a no-function
any ideas?
Upvotes: 1
Views: 9983
Reputation: 78792
From the signature:
ldply(.data, .fun = NULL, ..., .progress = "none",
.inform = FALSE, .parallel = FALSE, .paropts = NULL)
ldply
doesn't take a .variables
argument like dlply
(and, it's not implied by the ...
as they are passed as args to .fun
).
Pretty sure this gets what you want (and, I'm fairly sure there are better ways to do it):
extract_SEAc <- function(e) {
data.frame(e$xSEAc, e$ySEAc)
}
e2 <- ldply(ell, extract_SEAc)
head(e2)
Site SizeClass e.xSEAc e.ySEAc
1 1 Big -2.462496 12.28447
2 1 Big -2.473344 12.24886
3 1 Big -2.484814 12.21326
4 1 Big -2.496901 12.17769
5 1 Big -2.509601 12.14216
6 1 Big -2.522911 12.10669
Upvotes: 4