0

I have created a function which uses 2 variables to do grouping and uses third variable to create min and max for each groups. But the min and max function gives wrong output. It gives the minimum and maximum for entire dataset and not for each group.

myfunction= function(x,a,b,column) {
  temp=group_by(x,x[[a]],x[[b]])
   Score=summarise(temp,Totals=n(),Mnscore=min(x[[c]]),Mxscore=max(x[[c]]))
 return(Score)
}

myfunction(dataset,"a","b","c")


Actual Results:
a b Totals  Min Max
1 1  10     15  50
1 2  20     15  50
1 3  30     15  50

Expected results:
a b Totals  Min Max
1 1  10     20  48
1 2  20     21  49
1 3  30     15  50
1
  • you need to learn about lazy evaluations and quasi quotations in R. Commented Oct 11, 2019 at 18:23

3 Answers 3

1

to write a function, you could do the following:

library(tidyverse)
myfunction= function(x,a,b,column) 
  {
  column <- enquo(column)
  vars <- enquos(a,b)
  x %>% 
    group_by(!!!vars) %>% 
    summarise(Totals=n(),Mnscore=min(!!c),Mxscore=max(!!column))
}

then call this inputing a,b,c as symbols and not characters

myfunction(dataset,a,b,column)
Sign up to request clarification or add additional context in comments.

2 Comments

rlang 0.4.0 added a new curly-curly operator, which is effectively a shortcut for !!enquo(). So, now you can just do ... %>% summarize( Totals=n(), Mnscore = min({{column}}), Mxscore=max({{column}}) ). (Also, it is generally not advisable to use c as a variable name because of its conflict with the built-in c().)
@ArtemSokolov true its not advisable to use c.
1

You can use the data.table package if you want a very effective way to solve your problem. Try the following minimal reproducible example.

library(data.table)
set.seed(20191011L)

data <- data.table(
  V1 = letters[sample(3, 20, TRUE)],
  V2 = letters[sample(3, 20, TRUE)],
  V3 = runif(20)
)

fun <- function(data, groups, target){

  data[, .(min=min(get(target)), max=max(get(target))), mget(groups)]

}

fun(data, c("V1", "V2"), "V3")

##     V1 V2        min       max
##  1:  b  c 0.20653948 0.4618063
##  2:  a  a 0.09560888 0.3347064
##  3:  b  b 0.75071480 0.7507148
##  4:  c  a 0.66410519 0.8258410
##  5:  c  c 0.01303751 0.7635212
##  6:  a  b 0.04770186 0.6332439
##  7:  b  a 0.25069813 0.9008885

4 Comments

I want to create as a function since I have to use that function multiple times.
Sorry, did not saw you asked for a function. I edit my answer to implement it as a function.
I am getting this error : Error in .(min = min(get(target)), max = max(get(target))) : could not find function "."
Make sure your data is a data.table and not a data.frame
0

Try this:

require(dplyr)

result = dataset %>% 
  dplyr::group_by(a,b) %>% 
  dplyr::summarise(Totals = n(),
                   Mnscore = min(c),
                   Mxscore = max(c))

Let me know if it works.

3 Comments

how is this a function?
@Onyambu, it's not but OP does not necessarily need a function here.
I want to create as a function since I have to use that function multiple times.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.