group-by

Handling a missing string value to a function when using group_by in dplyr

五迷三道 提交于 2020-05-14 07:17:42
问题 I'm looking to create a function that can take multiple string inputs (2 in this example), and using group_by, return results even if only one string is input. I know I could create if statements to get around the case when only one string is passed to the function, but is there a better way for group_by to still produce output without building in conditional language (i.e., gets more cumbersome with multiple inputs). Reproducible example library(dplyr) # Create simple function car_fx <-

Rowwise operation within for loop using dplyr

偶尔善良 提交于 2020-05-12 07:58:16
问题 I have some transport data which I would like to perform a rowwise if comparison within a for loop. The data looks something like this. # Using the iris dataset > iris <- as.data.frame(iris) > head(iris) Sepal.Length Sepal.Width Petal.Length Petal.Width Species 1 5.1 3.5 1.4 0.2 setosa 2 4.9 3.0 1.4 0.2 setosa 3 4.7 3.2 1.3 0.2 setosa 4 4.6 3.1 1.5 0.2 setosa 5 5.0 3.6 1.4 0.2 setosa 6 5.4 3.9 1.7 0.4 setosa Where the result would record the instances of sepal lengths with equal petal width

Rowwise operation within for loop using dplyr

久未见 提交于 2020-05-12 07:57:49
问题 I have some transport data which I would like to perform a rowwise if comparison within a for loop. The data looks something like this. # Using the iris dataset > iris <- as.data.frame(iris) > head(iris) Sepal.Length Sepal.Width Petal.Length Petal.Width Species 1 5.1 3.5 1.4 0.2 setosa 2 4.9 3.0 1.4 0.2 setosa 3 4.7 3.2 1.3 0.2 setosa 4 4.6 3.1 1.5 0.2 setosa 5 5.0 3.6 1.4 0.2 setosa 6 5.4 3.9 1.7 0.4 setosa Where the result would record the instances of sepal lengths with equal petal width

How to Fetch records based on the Dates in MYSQL

爱⌒轻易说出口 提交于 2020-05-09 15:47:30
问题 I have sample data ID Name Amount cal_amt Run_amt Dates 1 Test 15000 0.00 15000 2020-06-01 1 Test 15000 0.00 30000 2020-04-01 1 Test 15000 12000 30000 2020-05-01 2 Test_1 18000 0.00 25000 2020-06-01 2 Test_1 18000 0.00 35000 2020-04-01 2 Test_1 18000 16000 35000 2020-05-01 I need to get MAX(month) of Run_Amount i.e : 2020-06-01 --> 15000 Need to fetch cal_amt of current month i.e : 2020-05-01 --> 12000 and 0.00 also relates to this month 2020-04-01 I need to get output like this : ID Name

Group by in mongoDB with property construction

老子叫甜甜 提交于 2020-05-09 06:26:27
问题 I have a collection named Accounts like below data /* 1 */ { "_id" : ObjectId("5e93fea52f804ab99b54e4a2"), "account" : "first", "connect" : "Always", "desc" : "first account feature first phase", "status" : true } /* 2 */ { "_id" : ObjectId("5e93fea32c804ab99b12e4d1"), "account" : "second", "connect" : "Sometimes", "desc" : "second account feature first phase", "status" : true } /* 3 */ { "_id" : ObjectId("5e93fea52f804ab99b55a7b1"), "account" : "first", "connect" : "Sometimes", "desc" :

Group by in mongoDB with property construction

僤鯓⒐⒋嵵緔 提交于 2020-05-09 06:26:13
问题 I have a collection named Accounts like below data /* 1 */ { "_id" : ObjectId("5e93fea52f804ab99b54e4a2"), "account" : "first", "connect" : "Always", "desc" : "first account feature first phase", "status" : true } /* 2 */ { "_id" : ObjectId("5e93fea32c804ab99b12e4d1"), "account" : "second", "connect" : "Sometimes", "desc" : "second account feature first phase", "status" : true } /* 3 */ { "_id" : ObjectId("5e93fea52f804ab99b55a7b1"), "account" : "first", "connect" : "Sometimes", "desc" :

Group by in mongoDB with property construction

天涯浪子 提交于 2020-05-09 06:26:00
问题 I have a collection named Accounts like below data /* 1 */ { "_id" : ObjectId("5e93fea52f804ab99b54e4a2"), "account" : "first", "connect" : "Always", "desc" : "first account feature first phase", "status" : true } /* 2 */ { "_id" : ObjectId("5e93fea32c804ab99b12e4d1"), "account" : "second", "connect" : "Sometimes", "desc" : "second account feature first phase", "status" : true } /* 3 */ { "_id" : ObjectId("5e93fea52f804ab99b55a7b1"), "account" : "first", "connect" : "Sometimes", "desc" :

How to select top N rows for each group in a Entity Framework GroupBy with EF 3.1

拟墨画扇 提交于 2020-05-09 05:37:08
问题 I need to get top 10 rows for each group in a table with entity framework. Based on other solution on SO, I tried 2 things: var sendDocuments = await context.Set<DbDocument> .Where(t => partnerIds.Contains(t.SenderId)) .GroupBy(t => t.SenderId) .Select(t => new { t.Key, Documents = t.OrderByDescending(t2 => t2.InsertedDateTime).Take(10) }) .ToArrayAsync(); error: System.InvalidOperationException: 'The LINQ expression '(GroupByShaperExpression: KeySelector: (d.SenderId), ElementSelector:

Count distinct by group- moving window

橙三吉。 提交于 2020-05-08 15:38:11
问题 Let's say I have a dataset contain visits in a hospital. My goal is to generate a variable that counts the number of unique patients the visitor has seen before at the date of the visit. I often work with group_by by dplyr but this seems a little tricky. I guess I would have to use group_by, n_distinct, and sum or some kind moving window command. The "goal" variable is what I need. visitor visitdt patient goal 125469 1/12/2018 15200 1 125469 1/19/2018 15200 1 125469 2/16/2018 15200 1 125469 2

Group by columns and summarize a column into a list

时光怂恿深爱的人放手 提交于 2020-05-07 12:07:11
问题 I have a dataframe like this: sample_df<-data.frame( client=c('John', 'John','Mary','Mary'), date=c('2016-07-13','2016-07-13','2016-07-13','2016-07-13'), cluster=c('A','B','A','A')) #sample data frame client date cluster 1 John 2016-07-13 A 2 John 2016-07-13 B 3 Mary 2016-07-13 A 4 Mary 2016-07-13 A I would like to transform it into different format, which will be like: #ideal data frame client date cluster 1 John 2016-07-13 c('A,'B') 2 Mary 2016-07-13 A For the 'cluster' column, it will be a