in Education by
I have a datatable with 3 categorical variables: primarydx, NumDx, and ContractType. primarydx <- rep(c("AB","MT"),10) NumDx <- rep(c(1,2,3), length.out=20 ) ContractType <- rep( c("W","L","W","W","W") , length.out=20) SecDx3 <- data.table(primarydx, NumDx, ContractType) I want to calculate the percentage of records of each ContractType within primarydx and NumDx. The following code works: Num <-SecDx3[ , .(n=.N), by=.(primarydx, NumDx, ContractType)] denom <- SecDx3[ , .(d=.N), by=.(primarydx, NumDx)] pct <- merge(Num,denom) pct[,pct:=n/d][] Is there a better way? I did find something like: SecDx3[,{ d =.N .SD[, .(pct = .N/d), by=.(primarydx, NumDx, ContractType)] }, by=.(primarydx, NumDx)] but this returns an error "The items in the 'by' or 'keyby' list are length (1,1,20). Each must be same length as rows in x or number of rows returned by i (20)." which I don't understand. Thanks for your help, David JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Probably faster will be SecDx3[, d:=.N, by=.(primarydx, NumDx) ][, { n<-.N .(n=n, d=d[1L], pct=n/d[1L]) }, by=.(primarydx, NumDx, ContractType)]

Related questions

0 votes
    I would like to call a .net 4.0 dll from an IIS7 application that is running in a .net 2.0 ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 26, 2022 in Education by JackTerrance
0 votes
    Is there a better way to see what guests have access to within SharePoint at the content level?...
asked Mar 10, 2021 in Technology by JackTerrance
0 votes
    The percentages of marks obtained by a student in six unit tests are given below: Unit test I II III IV V VI ... marks in the test? Select the correct answer from above options...
asked Nov 23, 2021 in Education by JackTerrance
0 votes
    Is there a better way than this to splice an array into another array in javascript var string = ' ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 10, 2022 in Education by JackTerrance
0 votes
    I know we are all using a bunch of ORMs for SQL, but I wanted to give a try to native drivers. ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    If only one 'Consumer Group' subscribed for a topic and there are lots of consumers in this Consumer Group, messages ... group. This is (1)Queuing model (2)Publish-Subscribe model...
asked Jun 17, 2021 in Technology by JackTerrance
0 votes
    I have a table with the following structure: yyyymm Revenue ... ... 201701 450 201701 600 201702 ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 21, 2022 in Education by JackTerrance
0 votes
    I have a table with the following structure: yyyymm Revenue ... ... 201701 450 201701 600 201702 ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 21, 2022 in Education by JackTerrance
0 votes
    Which of the following is the first calculating device? A. Abacus B. Calculator C. Turing Machine D. Pascaline Select the correct answer from above options...
asked Dec 31, 2021 in Education by JackTerrance
0 votes
    1. Name the first known calculating device used by baby loniya Select the correct answer from above options...
asked Dec 15, 2021 in Education by JackTerrance
0 votes
    Which of the following tool is used for constructing confidence intervals and calculating standard errors for ... answers pdf, Data Science interview questions for beginners...
asked Oct 30, 2021 in Education by JackTerrance
0 votes
    Which is the equation used for calculating unit weight of steel bar?...
asked Jan 20, 2021 in Technology by JackTerrance
...