case-when

Making tidyeval function inside case_when

白昼怎懂夜的黑 提交于 2020-07-21 03:06:28
问题 I have a data set that I like to impute one value among others based on probability distribution of those values. Let make some reproducible example first library(tidyverse) library(janitor) dummy1 <- runif(5000, 0, 1) dummy11 <- case_when( dummy1 < 0.776 ~ 1, dummy1 < 0.776 + 0.124 ~ 2, TRUE ~ 5) df1 <- tibble(q1 = dummy11) here is the output: df1 %>% tabyl(q1) q1 n percent 1 3888 0.7776 2 605 0.1210 5 507 0.1014 I used mutate and sample to share value= 5 among value 1 and 2 like this: df1 %

R supplying arguments while using case_when (R vectorization)

为君一笑 提交于 2020-06-27 15:48:08
问题 This is a follow up question to a question that I asked before (R apply multiple functions when large number of categories/types are present using case_when (R vectorization)). Unfortunately I have not been able to figure out the problem. I think I may have narrowed down the source of the problem an wanted to check if someone with a better understanding than me could help me figure out a solution. Suppose I have the following dataset: set.seed(100) City=c("City1","City2","City2","City1")

R supplying arguments while using case_when (R vectorization)

孤人 提交于 2020-06-27 15:48:08
问题 This is a follow up question to a question that I asked before (R apply multiple functions when large number of categories/types are present using case_when (R vectorization)). Unfortunately I have not been able to figure out the problem. I think I may have narrowed down the source of the problem an wanted to check if someone with a better understanding than me could help me figure out a solution. Suppose I have the following dataset: set.seed(100) City=c("City1","City2","City2","City1")

Oracle SQL CASE WHEN ORA-00932: inconsistent datatypes: expected CHAR got NUMBER 00932. 00000 - “inconsistent datatypes: expected %s got %s”

这一生的挚爱 提交于 2020-06-12 06:05:08
问题 Getting error ORA-00932: inconsistent datatypes: expected CHAR got NUMBER 00932. 00000 - "inconsistent datatypes: expected %s got %s" When i run the following query SELECT distinct CASE when t.cancelled = 'TRUE' then '0' else t.amount END AMOUNT, FROM table t If i run it with either a number or text for the else output, like this, it works. SELECT distinct CASE when t.cancelled = 'TRUE' then '0' else 'xxx' END AMOUNT, FROM table t 回答1: Use 0 instead of '0' . Amount is a number, and numbers

Import 'xml' node into sql sever dynamically with conditional statement

南楼画角 提交于 2020-01-24 23:40:52
问题 This is related to this question: Import 'xml' into Sql Server How would you deal with optional XML node, let's say "IsFixed" is an optional node and it exists in one file and it doesn't exist in another one, how to put condition in the code in order not to execute "Events.value" and avoid having Null column? DECLARE @XML XML = ' <EventSchedule> <Event Uid="2" Type="Main Event"> <EventKind>MainEvent</EventKind> <Fields> <Parameter Name="Type" Value="TV_Show"/> <Parameter Name="Name" Value=

SQL- Summing Case/When values from different fields

被刻印的时光 ゝ 提交于 2020-01-07 02:55:28
问题 I am trying to SUM values generated from a CASE/WHEN statement. The only problem is that the values come from different fields. Example: This is a set of overhead values from one item out of many in my query. In this example, I need to SUM the overhead values 14.850000 and 1.166600 to 16.016. The vac_type column determines the type of costs. In this case, when vac_type = WO1 or WO2, they are overhead costs. W10 is not, thus the NULL value. In all, there are 12 vac_types that are overhead

Returning the original value if it doesn't match any of the when_expressions

让人想犯罪 __ 提交于 2020-01-05 07:45:18
问题 I have a line on my SQL export field, and I am trying to remove return a blank value in a field if it exactly matches "AMA" in the last name. However, when I export the file with the current code, and names without "AMA" as the last name are fully blanked out. ,Max( Orders.ShipFirstName + ' ' + CASE Orders.ShipLastName WHEN 'AMA' THEN '' END ) As ShipFullName Right now, if someone is "Ray Miller", ShipFullName will be returned as blank. But if their name is "Ray AMA", then their name is

SQL Return 1,0 in new variable based on case when statement referring to multiple other variables

戏子无情 提交于 2019-12-31 04:56:26
问题 I'm trying to create a new variable that populates with a 1 (true), 0 (false) in MySQL based on a series of dates and ladder levels (null-E). See fiddle: http://sqlfiddle.com/#!9/9975e1 Where the record_dates and ladder_levels aren't necessarily in sequential order. I'd like to return the ladder_change fieldvia a case when (?) statement, that says something like: First, look only within matching IDs (i.e. just for ID 324) Then, something like: case when record_date2 > record_date1 AND (ladder

How do I use T-SQL's Case/When?

ε祈祈猫儿з 提交于 2019-12-29 03:24:09
问题 I have a huge query which uses case/when often. Now I have this SQL here, which does not work. (select case when xyz.something = 1 then 'SOMETEXT' else (select case when xyz.somethingelse = 1) then 'SOMEOTHERTEXT' end) (select case when xyz.somethingelseagain = 2) then 'SOMEOTHERTEXTGOESHERE' end) end) [ColumnName], Whats causing trouble is xyz.somethingelseagain = 2 , it says it could not bind that expression. xyz is some alias for a table which is joined further down in the query. Whats

Multi-level parsing text

可紊 提交于 2019-12-25 18:39:02
问题 The last time I had a problem: Parsing and structuring of a text file Now I imagine complicated conditions. For example. I had a text file with next contain: Head 1 Subhead 1 a 10 b 14 c 88 Subhead 2 a 15 b 16 c 17 d 88 Subhead 3 a 55 b 36 c 87 Head 4 Subhead 1 r 32 t 55 s 79 r 22 t 88 y 53 o 78 p 90 m 44 Head 53 Subtitle 1 y 22 b 33 Subtitle 2 a 88 g 43 r 87 Head 33 Subhead 1 z 11 d 66 v 88 b 69 Head 32 Subhead 1 n 88 m 89 b 88 Subhead 2 b 88 m 43 Now I need structure text to next plane. I