2

I'm writing a R function with aggregations using data.table package. My table looks like:

Name1   Name2   Price
  A       F      6
  A       D      5
  A       E      2
  B       F      4
  B       D      7
  C       F      4
  C       E      2

My function looks like:

MyFun <- function(Master_Table, Desired_Column, Group_By){
  Master_Table <- as.data.table(Master_Table)
  Master_Table_New <-  Master_Table[, (Master_Table$Desired_Column), by=.(Desired_Column$Group_By)]
  return(Master_Table_New)
}

I want to calculate df[, .(Group_Median = median(Price), by=.(Name1, Name2)] But when I apply it into my own function, it keeps giving me errors like: `

Error in `[.data.table`(Master_Table, , .(Med_Group = mean(Master_Table$Desired_Column)),  : 
  column or expression 1 of 'by' or 'keyby' is type NULL. Do not quote column names. Usage: DT[,sum(colC),by=list(colA,month(colB))] `

or:

Error in `[.data.table`(Master_Table, , .(Med_Group = mean(Master_Table$Desired_Column)),  : 
  column or expression 1 of 'by' or 'keyby' is type NULL. Do not quote column names. Usage: DT[,sum(colC),by=list(colA,month(colB))] 

This would be the very first step of my whole work. If anyone knows anything about this, please let me know, any help would be appreciated!

4
  • Why are you writing a function? Commented Apr 6, 2018 at 21:44
  • The function will contain more things, but the data.table part would be the base. Commented Apr 6, 2018 at 21:49
  • Why don't you group with dplyr::group_by, then use mutate to calculate Group_Median? Commented Apr 6, 2018 at 21:52
  • @M.zz.M I understood. You need to modify your function as mentioned in my answer so that it will work properly. I have given an example as well. Commented Apr 6, 2018 at 22:00

1 Answer 1

2

The function should be written as:

MyFun <- function(Master_Table, Desired_Column, Group_By){
  Master_Table[, sapply(.SD, mean),  .SDcols = Desired_Column, by=Group_By]
}

#Have a close watch here how Group_By is prepared to provide multiple columns.
MyFun(DT, "Price", "Name1,Name2")
#     Name1 Name2 V1
# 1:     A     F  6
# 2:     A     D  5
# 3:     A     E  2
# 4:     B     F  4
# 5:     B     D  7
# 6:     C     F  4
# 7:     C     E  2

Data

DT <- read.table(text = 
"Name1   Name2   Price
A       F      6
A       D      5
A       E      2
B       F      4
B       D      7
C       F      4
C       E      2",
header = TRUE, stringsAsFactors = FALSE)

setDT(DT)
Sign up to request clarification or add additional context in comments.

7 Comments

Hi MKR, really appreciate for your help! It is exactly what I want! But I still have a question -- why should I modify my function before using it? Is it a special thing for data table when I want to insert it into a self-defined function?
@M.zz.M There are two ways. The function written by me expects a data.table type argument. Hence setDT call has been used to convert it to data.table. Another option is to call setDT within function itself. Since you had mentioned about data.table hence I kept my function expecting a data.table. Hope it clarifies your doubt.
@M.zz.M Also, as you can make out that there is no need of a function in your case. The one line which is part of my function can be used instead of function.
@M.zz.M Though you are relatively new user to SO hence wanted to inform about this link stackoverflow.com/help/someone-answers . Probably you are aware about information on link.
Thank you for the advice and explanations! As a relatively new user, it definitely helps me a lot! Also, I rewrite the Group_By part in order to merge table. Following is my code: MyFun <- function(Master_Table, Desired_Column, Group_By_A, Group_By_B) and make Group_By = c(Group_By_A, Group_By_B) I just wondering if you were me, would you have any more efficient solutions? Thank you.
|

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.