The following is the kind of data "types/structure" i'm working with, it includes 3 factor variables.
library(data.table)
library(ggplot2)
DT <- data.table(mtcars)
DT[["cyl"]] <- factor(DT[["cyl"]])
DT[["gear"]] <- factor(DT[["gear"]])
DT[["vs"]] <- factor(DT[["vs"]])
DT <- DT[, c("cyl", "gear", "vs"), with=F]
setkey(DT, cyl, gear, vs)
Context I've been using this function to aggregate data with data.table
interactively and works just fine. The problem is when i try to include it in another function. I don't have much experience programming, so any guidence will be greatly appreciated. I imagine this has to do with enviroments, and how the arguments are passed but i don't really know how to solve it.
grp <- function(x) {
percentage = as.numeric(table(x)/length(x))
list(x = levels(x),
percentage = percentage,
label = paste( round( as.numeric(table(x)/length(x), 0 ) * 100 ), "%")
)
}
This would be the expected output:
DT_agg <- DT[, grp(cyl), by=vs]
Question The idea of the second function is to take a data.frame/data.table
object, apply the previous function including the option to use one or two grouping variables. The final idea, would be to include this last object in a ggplot() call, and use the grouping variables as facets, calling the agg() function from the ggplot() call.
agg <- function(data, x, groupby1, groupby2 = NULL,...){
data = substitute(data)
x = substitute(x)
groupby1 = substitute(groupby1)
groupby2 = substitute(groupby2)
if(is.null(groupby2)){
DT_agg = data[, grp(x), by=groupby1]
} else {
DT_agg = data[, grp(x), by=groupby1,groupby2]
}
DT_agg
}
agg(data = DT, x = cyl, groupby1 = vs)
Error in unique.default(x, nmax = nmax) :
unique() applies only to vectors
agg <- function(data, x, groupby1, groupby2 = NULL,...){
data = eval(substitute(data))
x = substitute(data$x) # changed this bit (it was producing an error)
groupby1 = substitute(groupby1)
groupby2 = substitute(groupby2)
if(is.null(eval(substitute(groupby2)))) {
eval(data)[, grp(eval(x)), by=groupby1]
} else {
eval(data)[, grp(eval(x)), by=list(eval((groupby1)),eval(groupby2))]
}
}
For some reason the provided solution in the answer isn't working for me. Agstudy's agg()
provides an answer, it runs but the output isn't. I've tried a few changes, but it's not working right.
Using the grp() function defined above i get this result that's correct:
ok = DT[, grp(cyl), by = vs]
print(ok)
# vs x percentage label
# 1: 1 4 0.71428571 71%
# 2: 1 6 0.28571429 29%
# 3: 1 8 0.00000000 0%
# 4: 0 4 0.05555556 6%
# 5: 0 6 0.16666667 17%
# 6: 0 8 0.77777778 78%
Using agstudy version of agg()
i get this that's not correct:
not_ok = agg(DT, cyl, vs)
print(not_ok)
# groupby1 x percentage label
# 1: 1 4 0.34375 34%
# 2: 1 6 0.21875 22%
# 3: 1 8 0.43750 44%
# 4: 0 4 0.34375 34%
# 5: 0 6 0.21875 22%
# 6: 0 8 0.43750 44%
I wonder how can the function work correctly on it's own (1st case) and not inside the agg function.
Excellent question ! specially for somemone who don't have much experience programming.
Using eval
and simplifying your function ( no need to assign the data.table):
agg <- function(data, x, groupby1, groupby2 = NULL,...){
data = substitute(data)
x = substitute(x)
groupby1 = substitute(groupby1)
groupby2 = substitute(groupby2)
if(is.null(groupby2)) eval(data)[, grp(eval(x)), by=groupby1]
else eval(data)[, grp(eval(x)),
by=list(eval((groupby1)),eval(groupby2))]
}
testing it :
agg(data = DT, x = cyl, groupby1 = vs,groupby2 = gear )
## groupby1 groupby2 x percentage label
## 1: 1 3 4 0.3333 33 %
## 2: 1 3 6 0.6667 67 %
## 3: 1 3 8 0.0000 0 %
## 4: 1 4 4 0.8000 80 %
## 5: 1 4 6 0.2000 20 %
## 6: 1 4 8 0.0000 0 %
## 7: 0 5 4 0.2500 25 %
## 8: 0 5 6 0.2500 25 %
## 9: 0 5 8 0.5000 50 %
## 10: 1 5 4 1.0000 100 %
## 11: 1 5 6 0.0000 0 %
## 12: 1 5 8 0.0000 0 %
## 13: 0 4 4 0.0000 0 %
## 14: 0 4 6 1.0000 100 %
## 15: 0 4 8 0.0000 0 %
## 16: 0 3 4 0.0000 0 %
## 17: 0 3 6 0.0000 0 %
## 18: 0 3 8 1.0000 100 %
I believe this is working with the 1.8.11 version. Perhaps something changed in the dev version.
library(data.table)
library(ggplot2)
DT <- data.table(mtcars)
DT[["cyl"]] <- factor(DT[["cyl"]])
DT[["gear"]] <- factor(DT[["gear"]])
DT[["vs"]] <- factor(DT[["vs"]])
DT <- DT[, c("cyl", "gear", "vs"), with=F]
setkey(DT, cyl, gear, vs)
grp <- function(x) {
percentage = as.numeric(table(x)/length(x))
list(x = levels(x),
percentage = percentage,
label = paste( round( as.numeric(table(x)/length(x), 0 ) * 100 ), "%")
)
}
agg <- function(data, x, groupby1, groupby2 = NULL,...){
data = substitute(data)
x = substitute(x)
groupby1 = substitute(groupby1)
groupby2 = substitute(groupby2)
if(is.null(groupby2)) eval(data)[, grp(eval(x)), by=groupby1]
else eval(data)[, grp(eval(x)),
by=list(eval((groupby1)),eval(groupby2))]
}
ok = DT[, grp(cyl), by = vs]
print(ok)
ok2 = agg(DT, cyl, vs)
print(ok2)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.