group-by

dplyr count non-NA value in group by [duplicate]

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-17 11:03:15
问题 This question already has answers here : R group by, counting non-NA values (3 answers) Closed last year . Here is my example mydf<-data.frame('col_1'=c('A','A','B','B'), 'col_2'=c(100,NA, 90,30)) I would like to group by col_1 and count not-NA elements in col_2 I would like to do it with dplyr . Here is what I tried after searching SO: mydf %>% group_by(col_1) %>% summarise_each(funs(!is.na(col_2))) mydf %>% group_by(col_1) %>% mutate(non_na_count = length(col_2, na.rm=TRUE)) mydf %>% group

dplyr count non-NA value in group by [duplicate]

自古美人都是妖i 提交于 2019-12-17 11:02:00
问题 This question already has answers here : R group by, counting non-NA values (3 answers) Closed last year . Here is my example mydf<-data.frame('col_1'=c('A','A','B','B'), 'col_2'=c(100,NA, 90,30)) I would like to group by col_1 and count not-NA elements in col_2 I would like to do it with dplyr . Here is what I tried after searching SO: mydf %>% group_by(col_1) %>% summarise_each(funs(!is.na(col_2))) mydf %>% group_by(col_1) %>% mutate(non_na_count = length(col_2, na.rm=TRUE)) mydf %>% group

What is the correct way to do a HAVING in a MongoDB GROUP BY?

≡放荡痞女 提交于 2019-12-17 10:53:46
问题 For what would be this query in SQL (to find duplicates): SELECT userId, name FROM col GROUP BY userId, name HAVING COUNT(*)>1 I performed this simple query in MongoDB: res = db.col.group({key:{userId:true,name:true}, reduce: function(obj,prev) {prev.count++;}, initial: {count:0}}) I've added a simple Javascript loop to go over the result set, and performed a filter to find all the fields with a count > 1 there, like so: for (i in res) {if (res[i].count>1) printjson(res[i])}; Is there a

group by first character

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-17 10:47:54
问题 I have a problem with a query in Oracle SQL. I have a first_name column in an employees table. I want to group my records according to the first character in first_name . For example, I have 26 records, one with name = 'Alice' , one with name = 'Bob' , and so on down the alphabet for each name's first character. After the query, there should be 26 groups with one employee each. I tried the following, but it's not working: SELECT employee_id, (SUBSTR(first_name,1,1)) AS alpha FROM employees

Conversion failed when converting the varchar value 'simple, ' to data type int

我只是一个虾纸丫 提交于 2019-12-17 10:47:52
问题 I am struggling for a few days with this issue and I can't figure out how can I fix it. I would like to group by my table on values 1 , 2 , 3 , 4 , 5 so I have created a temporary table with this values. Now I have to INNER JOIN this table with other tables on a.value = #myTempTable.num . BUT a.value is ntext so I need to CONVERT it what I actually did, but I am getting an error: Conversion failed when converting the varchar value 'simple, ' to data type int. (on line 7) Create table

MySQL groupwise MAX() returns unexpected results

无人久伴 提交于 2019-12-17 09:58:10
问题 TABLE: LOAN Loan_no Amount SSS_no Loan_date 7 700.00 0104849222 2010-01-03 8 200.00 0104849222 2010-02-28 9 300.00 0119611199 2010-11-18 10 150.00 3317131410 2012-11-28 11 600.00 0104849222 2011-01-03 14 175.00 3317131410 2012-12-05 15 260.00 3317131410 2013-02-08 16 230.00 0104849222 2013-03-06 17 265.00 0119611199 2011-04-30 18 455.00 3317131410 2013-03-10 DESIRED RESULTS: I would want to retrieve the latest loan availed off by each person (identified by their SSS number). The results

ILookup<TKey, TVal> vs. IGrouping<TKey, TVal>

旧城冷巷雨未停 提交于 2019-12-17 07:11:08
问题 I've been having trouble articulating the differences between ILookup<TKey, TVal> and IGrouping<TKey, TVal>, and am curious if I understand it correctly now. LINQ compounded the issue by producing sequences of IGrouping items while also giving me a ToLookup extension method. So it felt like they were the same until I looked more closely. var q1 = from n in N group n by n.MyKey into g select g; // q1 is IEnumerable<IGrouping<TKey, TVal>> Which is equivalent to: var q2 = N.GroupBy(n => n.MyKey,

MySQL GROUP BY behavior

倖福魔咒の 提交于 2019-12-17 06:43:31
问题 Given the following table ' foo ' ID | First Name | Last Name ---------------------------- 67 John Smith ---------------------------- 67 Bill Jacobs What first_name and last_name will the following query return and why? SELECT * FROM foo WHERE ID = 67 GROUP BY ID 回答1: MySQL chooses a row arbitrarily. In practice, commonly used MySQL storage engines return the values from the first row in the group, with respect to the physical storage. create table foo (id serial primary key, category varchar

AngularJS Group By Directive without External Dependencies

泪湿孤枕 提交于 2019-12-17 06:29:50
问题 I'm new to Angular and would like to learn the best way to handle a problem. My goal is to have a reusable means to create group by headers. I created a solution which works, but I think this should be a directive instead of a scope function within my controller, but I'm not sure how to accomplish this, or if a directive is even the right way to go. Any inputs would be greatly appreciated. See my current approach working on jsFiddle In the HTML it's a simple list using ng-repeat where I call

Select all months within given date span, including the ones with 0 values

岁酱吖の 提交于 2019-12-17 06:16:08
问题 I'm trying to write a MySQL query to get an average value per month, for all months between to given dates. My idea is this: Query, something like SELECT AVG(value1) as avg_value_1, AVG(value2) as avg_value_2, MONTH(save_date) as month, YEAR(save_date) as year FROM myTable WHERE save_date BETWEEN '2009-01-01' AND '2009-07-01' GROUP BY YEAR(save_date), MONTH(save_date) avg_value_1 | avg_value_2 | month | year 5 | 4 | 1 | 2009 2 | 1 | 2 | 2009 7 | 5 | 3 | 2009 0 | 0 | 4 | 2009 <--- 6 | 5 | 5 |