aggregate

Return date range by group

陌路散爱 提交于 2019-12-20 03:18:05
问题 I want to group by color and calculate the date range for that color. I have tried group_by() , summarize() and aggregate() . #Data: df1 <- as.Date(c('Jul 1', 'Jun 26', 'July 5', 'July 15'), format = '%B %d') df2 <- c("red", "blue", "red", "blue") df1 <- data.frame(df1,df2) What I'm trying to get: # Group.1 x [1] 4 red [2] 19 blue I have been trying this: df <- aggregate(df1[,1], list(df1[,2]), as.numeric(max(df1[,1]) - min(df1[,1]), units="days")) I have tested as.numeric(max(df1[,1]) - min

EF multiple aggregate in single query

本秂侑毒 提交于 2019-12-20 02:45:17
问题 I want to get count of a set based on different condition: var invoices = new AccountingEntities().Transactions var c1 = invoices.Count(i=>i.Type = 0); var c2 = invoices.Count(i=>i.Type = 1); var c3 = invoices.Count(i=>i.Type = 2); How its possible to call all three queries in one DB round trip to increase performance? 回答1: Sure, just wrap up your three counts in a POCO or anonymous type: using (var invoices = new AccountingEntities()) { var c = (from i in invoices.Transactions select new {

How to calculate the mean in a data frame using aggregate function in R?

点点圈 提交于 2019-12-20 02:37:48
问题 I have a data frame df1: number=c(4,3,2,3,4,1) year=c("2000","2000","2000", "2015", "2015", "2015") items=c(12, 10, 15, 5, 10, 7) df1=data.frame(number, year, items) setDT(df1)[, Prop := number/sum(number), by = year] such that it looks like this: number year items Prop 1: 4 2000 12 0.4444444 2: 3 2000 10 0.3333333 3: 2 2000 15 0.2222222 4: 3 2015 5 0.3750000 5: 4 2015 10 0.5000000 6: 1 2015 7 0.1250000 I want to get the mean of the number of items per year, so I tried using this fuction:

Aggregating across list of dataframes and storing all results

戏子无情 提交于 2019-12-19 11:58:10
问题 I have a list of 9 data frames, each data frame having approx 100 rows and 5-6 cols. I want to aggregate the values in a col based on the groups specified in another col across all data frames and store all results in a separate data frame. To elucidate, consider a list [[1]] Date Group Age Nov A 13 Nov A 14 Nov B 9 Nov D 10 [[2]] Date Group Age Dec C 11 Dec C 12 Dec E 10 My code is as follows for (i in 1:length(list)){ x<-aggregate(list[[i]]$Age~list[[i]]$Group, list[[i]], sum) x<-rbind(x) }

Multiple functions in aggregate

依然范特西╮ 提交于 2019-12-19 10:08:10
问题 Is is possible that from the following data frame df1 Branch Loan_Amount TAT A 100 2.0 A 120 4.0 A 300 9.0 B 150 1.5 B 200 2.0 I can use aggregate function to get the following output as a dataframe df2 Branch Number_of_loans Loan_Amount Total_TAT A 3 520 15.0 B 2 350 3.5 I know I can use nrow to calculate the number_of_loans and merge, but I am looking for a better method. 回答1: With dplyr, you could do this: library(dplyr) group_by(d,Branch) %>% summarize(Number_of_loans = n(), Loan_Amount =

Adding a grouped, aggregate nunique column to pandas dataframe

白昼怎懂夜的黑 提交于 2019-12-19 08:18:11
问题 I want to add an aggregate, grouped, nunique column to my pandas dataframe but not aggregate the entire dataframe. I'm trying to do this in one line and avoid creating a new aggregated object and merging that, etc. my df has track, type, and id. I want the number of unique ids for each track/type combination as a new column in the table (but not collapse track/type combos in the resulting df). Same number of rows, 1 more column. something like this isn't working: df['n_unique_id'] = df

Weighted sum of variables by groups with data.table

和自甴很熟 提交于 2019-12-19 08:12:58
问题 I am looking for a solution to compute weighted sum of some variables by groups with data.table. I hope the example is clear enough. require(data.table) dt <- data.table(matrix(1:200, nrow = 10)) dt[, gr := c(rep(1,5), rep(2,5))] dt[, w := 2] # Error: object 'w' not found dt[, lapply(.SD, function(x) sum(x * w)), .SDcols = paste0("V", 1:4)] # Error: object 'w' not found dt[, lapply(.SD * w, sum), .SDcols = paste0("V", 1:4)] # This works with out groups dt[, lapply(.SD, function(x) sum(x * dt

GROUP BY consecutive dates delimited by gaps

試著忘記壹切 提交于 2019-12-19 05:24:00
问题 Assume you have (in Postgres 9.1 ) a table like this: date | value which have some gaps in it (I mean: not every possible date between min(date) and max(date) has it's row). My problem is how to aggregate this data so that each consistent group (without gaps) is treated separately, like this: min_date | max_date | [some aggregate of "value" column] Any ideas how to do it? I believe it is possible with window functions but after a while trying with lag() and lead() I'm a little stuck. For

Maven - 'all' or 'parent' project for aggregation?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-19 03:21:19
问题 For educational purposes I have set up a project layout like so (flat in order to suite eclipse better): -product | |-parent |-core |-opt |-all Parent contains an aggregate project with core, opt and all. Core implements the mandatory part of the application. Opt is an optional part. All is supposed to combine core with opt, and has these two modules listed as dependencies. I am now trying to make the following artifacts: product-core.jar product-core-src.jar product-core-with-dependencies

Column invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause

别等时光非礼了梦想. 提交于 2019-12-19 02:47:30
问题 We have a table which will capture the swipe record of each employee . I am trying to write a query to fetch the list of distinct employee record by the first swipe for today. We are saving the swipe date info in datetime column. Here is my query its throwing exception. select distinct [employee number], [Employee First Name] ,[Employee Last Name] ,min([DateTime]) ,[Card Number] ,[Reader Name] ,[Status] ,[Location] from [Interface].[dbo].[VwEmpSwipeDetail] group by [employee number] where