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

Use group_indices() in mutate() with nse #2160

Closed
benmarwick opened this issue Oct 3, 2016 · 6 comments
Closed

Use group_indices() in mutate() with nse #2160

benmarwick opened this issue Oct 3, 2016 · 6 comments

Comments

@benmarwick
Copy link
Contributor

Can we get group_indices to work in mutate using non-standard evaluation?

Often we want to add the output of group_indices back to the dataframe for further manipulation. It is possible with se, but it's a lot of typing and could be more novice-friendly.

This would be good if it would work:

mtcars %>%  mutate(group = group_indices(cyl))
Error: no applicable method for 'group_indices_' applied to an object of class "c('double', 'numeric')"

Instead we have to do this, using the se version:

  mtcars %>% mutate(group = group_indices_(., .dots="cyl"))
 mpg cyl  disp  hp drat    wt  qsec vs am gear carb group
1  21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4     2
2  21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4     2
3  22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1     1
4  21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1     2
5  18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2     3
6  18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1     2
7  14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4     3
8  24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2     1
9  22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2     1
10 19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4     2
11 17.8   6 167.6 123 3.92 3.440 18.90  1  0    4    4     2
@krlmlr
Copy link
Member

krlmlr commented Nov 7, 2016

With #2190, the following works for me:

mtcars %>%  mutate(., group = group_indices(., cyl))

@hadley: Should we simplify this any further?

@hadley
Copy link
Member

hadley commented Nov 7, 2016

That seems reasonable to me.

@krlmlr
Copy link
Member

krlmlr commented Nov 7, 2016

In particular: Should the following work:

mtcars %>%  mutate(group = group_indices(cyl))

@hadley
Copy link
Member

hadley commented Nov 7, 2016

I don't think so. We could develop a function that did work like that, but it would need to be a new function since it wouldn't be backward compatible.

@krlmlr
Copy link
Member

krlmlr commented Nov 7, 2016

Yeah, we already have row_number(x) vs. row_number() (#2218).

@sluedtke
Copy link

Hi,

I am still running into problems with that issue, Rstudio freezes (and restarts with a new session) and within vim (and vim-r-plugin) R crashes with *** Error in /usr/lib/R/bin/exec/R': free(): invalid pointer: 0x0000000009ff8ca8 *** `

library(dplyr)
temp = data.frame(a = rep(c(2, 3, 4), each=4), 
                  b = seq(1, 12))

# that fails as mentioned before
mutate(temp, id = group_indices(a))

# that fails, not expected 
temp %>% mutate(., id = group_indices(., a))

#that fails, not expected ;-) 
mtcars %>%  mutate(., group = group_indices(., cyl))

# that works
temp$id = group_indices(temp, a)

Session details:

> sessionInfo()
R version 3.3.2 (2016-10-31)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: KDE neon User Edition 5.8

locale:
 [1] LC_CTYPE=en_GB.UTF-8       LC_NUMERIC=C               LC_TIME=en_GB.UTF-8       
 [4] LC_COLLATE=en_GB.UTF-8     LC_MONETARY=en_GB.UTF-8    LC_MESSAGES=en_GB.UTF-8   
 [7] LC_PAPER=en_GB.UTF-8       LC_NAME=C                  LC_ADDRESS=C              
[10] LC_TELEPHONE=C             LC_MEASUREMENT=en_GB.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] dplyr_0.5.0    colorout_1.0-3

loaded via a namespace (and not attached):
[1] magrittr_1.5   R6_2.1.2       assertthat_0.1 DBI_0.4-1      tools_3.3.2    tibble_1.2    
[7] Rcpp_0.12.5   

Any help is appreciated.

@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
Development

No branches or pull requests

4 participants