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

a bug with group_by + mutate_each ? #2049

Closed
GodefroyClair opened this issue Jul 31, 2016 · 7 comments
Closed

a bug with group_by + mutate_each ? #2049

GodefroyClair opened this issue Jul 31, 2016 · 7 comments

Comments

@GodefroyClair
Copy link

@GodefroyClair GodefroyClair commented Jul 31, 2016

Hi,
With dplyr Version: 0.4.3 and R :
version 3.3.0 (2016-05-03)
Platform: x86_64-apple-darwin14.5.0 (64-bit)
Running under: OS X 10.11.6 (El Capitan)
this code :

set.seed(7)
df <- data.frame(x=10*runif(9), y=20*rnorm(9), group=rep(c("a","b","c"),3))
df2 <- df %>% group_by(group) %>% mutate_each(funs(scale))

passes ok and if I call df2, I can see its content. But if I do View(df2) or str(df2) I got an error

Error in FUN(X[[i]], ...) :
dims [product 3] do not match the length of object [9]

@GodefroyClair
Copy link
Author

@GodefroyClair GodefroyClair commented Sep 13, 2016

Can someone answer me on this please ? :)

@joranE
Copy link
Contributor

@joranE joranE commented Sep 14, 2016

The issue seems to be that scale returns a matrix not a vector, and somehow (in dplyr?) the dimension attribute is being set to be 3x1 for the whole column. Note that if you type df2[[1]] you only see three values, because the dimension attribute is sort of "lying".

A workaround might be to use my_scale <- function(x) c(scale(x)) instead to force a dimensionless vector to be returned.

@GodefroyClair
Copy link
Author

@GodefroyClair GodefroyClair commented Sep 16, 2016

Thank you for the answer JoranE. That was what I suspected but your proof and your solution are better than mine. Is there something else to do to report this now that we suspect quite strongly that it's a bug in dplyr ?

@jrosen48
Copy link

@jrosen48 jrosen48 commented Sep 21, 2016

@krlmlr
Copy link
Member

@krlmlr krlmlr commented Sep 24, 2016

I think this is a duplicate of #1918.

@krlmlr
Copy link
Member

@krlmlr krlmlr commented Sep 24, 2016

Can you please try devtools::install_github("krlmlr/dplyr@b-1918-grouped-dims")?

@vnijs
Copy link

@vnijs vnijs commented Sep 25, 2016

Possibly related issue:

> dat <- mtcars[1:5,1:3]
> df <- mutate_each(dat, funs(scale(.))) %>% mutate(somevar = cyl)
Error: Each variable must be a 1d atomic vector or list.
Problem variables: 'mpg', 'cyl', 'disp'

Using as.vector(scale(.)), suggested by @krlmlr, works

@krlmlr krlmlr closed this in d1c91f3 Sep 29, 2016
@lock lock bot locked as resolved and limited conversation to collaborators Jun 8, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
5 participants