case

Case Statement Equivalent in R

*爱你&永不变心* 提交于 2019-12-27 16:48:32
问题 I have a variable in a dataframe where one of the fields typically has 7-8 values. I want to collpase them 3 or 4 new categories within a new variable within the dataframe. What is the best approach? I would use a CASE statement if I were in a SQL-like tool but not sure how to attack this in R. Any help you can provide will be much appreciated! 回答1: case_when() , which was added to dplyr in May 2016, solves this problem in a manner similar to memisc::cases() . For example: library(dplyr)

Create graph to force Kruskal's algorithm to worst case [closed]

送分小仙女□ 提交于 2019-12-25 19:52:08
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 2 years ago . I wanna make a grpah with many nodes to force kruskal algorithm in worst case scenario. What kind of graph should it be ? I im stuck . 回答1: Supposing you are implementing kruskal algorithm with dijoint sets, and sorting the edges with a stable and "fast" algorithm you will have O

oracle equivalent of mysql ifnull (no case, no if)

倖福魔咒の 提交于 2019-12-25 16:24:31
问题 I am looking for a quick way to do SELECT IFNULL(columna, columnb) FROM mytable (I have dozens of columns and don't want to write a case for each of them) 回答1: You can also use the standard COALESCE keyword, which allows you to pass it multiple parameters: SELECT COALESCE(columna, columnb, ..., columnz) FROM mytable COALESCE keyword documentation 回答2: just found out: SELECT nvl(columna, columnb) FROM mytable http://docs.oracle.com/cd/B19306_01/server.102/b14200/functions105.htm 来源: https:/

Use case in where clause [duplicate]

时间秒杀一切 提交于 2019-12-25 11:56:15
问题 This question already has answers here : “CASE” statement within “WHERE” clause in SQL Server 2008 (11 answers) Closed 2 years ago . I am trying to add case statement to where condition in a query. My query looks like below- select * from table_name where if(id==1) then col1 = 0 else col1 is null As shown in above query I want to add col1 = 0 in where condition if id-==0 else I want col1 is null in where condition. I tried select * from table_name where case id =1 then (col1 = 0) else col1 is

Mysql CASE - WHEN - THEN - returning wrong data type (blob)

≡放荡痞女 提交于 2019-12-25 08:58:02
问题 Im creating customizable product attributes in a web store - each attribute can have different type of data, each data type is stored in a separate column using corresponding mysql datatype. I Have a query like: SELECT products.id AS id, products.sku AS sku, products.name AS name, products.url_key AS url_key, attributes.name AS attribute, CASE WHEN `attribute_types`.`type` = 'text' THEN product_attribute_values.value_text WHEN `attribute_types`.`type` = 'float' THEN product_attribute_values

Case in select after sum?

有些话、适合烂在心里 提交于 2019-12-25 06:35:17
问题 I'm trying to assign a different column to a sum depending on the dates. Here's what I have: SELECT SUM(amount) CASE WHEN date BETWEEN '1/1/13' AND '1/1/14' THEN Last_yr_tot WHEN date BETWEEN '1/1/14' AND 'now' THEN YTD My entire query. select fd.location_cd ,round(sum(case when fd.fs_dt between '1/1/13' and '12/31/13' then amount else 0 end)::numeric, 0) as Last_yr ,round(sum(case when fd.fs_dt between '1/1/14' and current_date then amount else 0 end)::numeric, 0) as YTD ,round(sum(case when

SQL, using entered time to find session with most visitors

戏子无情 提交于 2019-12-25 05:28:14
问题 I have a query that I need to write. Lab assistants will enter the information for students coming to the lab for help. the field are: Visit_ID, Student_ID, Visit_Date, Time_In, Time_Out, Course, Asst_ID. There are 3 sessions for time_in entry Session 1 9-12 Session 2 12-3 Session 3 3-6 The query needs to determine which session on the current day had the most visitors. This is what I have so far: with cte as ( select case when cast(Time_In as time) >'12:00:00' and cast(Time_In as time) <='15

How to combine cell value to column name with CASE logic in SQL

北城以北 提交于 2019-12-25 04:24:10
问题 I have two VIEWS where data is represented as below: VIEW 1 | username | function | level | location | |:-----------|------------:|:------------:| :-------------:| | John | Operation | Country | United Kingdom | | John | Operation | Area | South West | | John | Operation | Shop | 0001 | VIEW 2 | shop | region | country | area | |:-----------|------------:|:---------------:| :-------------:| | 1200 | u1 | United Kingdom | West | | 1201 | u2 | United Kingdom | West | | 1000 | f1 | France |

Aliases not working in ORDER BY clause

▼魔方 西西 提交于 2019-12-25 03:28:17
问题 This will work fine in MySQL 5: SELECT INSTR(foo, 'Bar') as foobar FROM Table ORDER BY CASE WHEN foobar = 1 THEN foo END DESC; Bu in MySQL 4, I get the error: ERROR 1054 (42S22): Unknown column 'foobar' in 'order clause' However, if I change the clause to this, it will work on both versions: SELECT INSTR(foo, 'Bar') as foobar FROM Table ORDER BY CASE WHEN INSTR(foo, 'Bar') = 1 THEN foo END DESC; To ensure compatibility, do I have to always use the second way? 回答1: because foobar is an alias.

Read an input number and redirect it to a column in a file

妖精的绣舞 提交于 2019-12-25 02:39:12
问题 I have a file with a table like this: I have done that my program returns me the values (max, min and mean) from the line of the gene that I'm looking for. Now my goal is the same, but instead of words, the user will print the number of the column. Ego to obtain these values but only from one column. Here there is my code: #!/bin/bash FICHERO="affy.txt" function OPTIONS { echo "_____________OPTIONS_____________" echo "" echo " 1. Select one gene and its test results" echo " 2. Select one