Reputation: 47
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
Upvotes: 0
Views: 242
Reputation: 79238
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)
Upvotes: 1
Reputation: 1345
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
Upvotes: 1
Reputation: 499
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.
Upvotes: 0