Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updating plyr to dplyr #75

Open
tavad opened this issue Mar 6, 2023 · 0 comments
Open

updating plyr to dplyr #75

tavad opened this issue Mar 6, 2023 · 0 comments

Comments

@tavad
Copy link

tavad commented Mar 6, 2023

I had some comparability problems between plyr and tidyverse (dplyr) in the following function that draws ellipses:

plyr implantation:

ell <- plyr::ddply(df.u, "groups", function(x) {
if (nrow(x) <= 2) {
return(NULL)
}
sigma <- var(cbind(x$xvar, x$yvar))
mu <- c(mean(x$xvar), mean(x$yvar))
ed <- sqrt(qchisq(ellipse.prob, df = 2))
data.frame(sweep(circle %*% chol(sigma) * ed, 2,
mu, FUN = "+"), groups = x$groups[1])
})

I have rewritten this function in dplyr It is a bit long, but it does the job and does not have comparability issues. The run time is practically the same.

ell <-
df.u %>%
group_by(groups) %>%
filter(n() > 2) %>%
summarize(
sigma = list(var(cbind(xvar, yvar))),
mu = list(c(mean(xvar), mean(yvar))),
ed = sqrt(qchisq(ellipse.prob, df = 2)),
circle_chol = list(circle %*% chol(sigma[[1]]) * ed),
ell = list(sweep(circle_chol[[1]], 2, mu[[1]], FUN = "+")),
xvar = map(ell, ~.x[,1]),
yvar = map(ell, ~.x[,2]),
.groups = "drop"
) %>%
select(xvar, yvar, groups) %>%
unnest(c(xvar, yvar))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant