conditional-statements

Sort data frame by two columns (with condition) [duplicate]

↘锁芯ラ 提交于 2019-11-28 09:37:26
问题 This question already has answers here : How to sort a dataframe by multiple column(s) (19 answers) Closed 4 years ago . I have the following data frame in R: DataTable <- data.frame( Name = c("Nelle","Alex","Thomas","Jeff","Rodger","Michi"), Age = c(17, 18, 18, 16, 16, 16), Grade = c(1,5,3,2,2,4) ) Name Age Grade 1 Nelle 17 1 2 Alex 18 5 3 Thomas 18 3 4 Jeff 16 2 5 Rodger 16 2 6 Michi 16 4 Now ill will sort this data frame by its Age column. No problem so far: DataTable_sort_age <- DataTable

Alternative to Switch Case in Java

↘锁芯ラ 提交于 2019-11-28 09:21:32
Is there any alternative way to implement a switch case in Java other than if else which is not looking good. A set of values will be there in combination, according to the selection corresponding method has to be executed. Presumably you're struggling with the requirement of case's being constant. Typically this is a code-smell, but there are things you can do. You might want to raise and link to another question that details why you're trying to switch. Map<String,Object> map = new HasMap<String,Object>(); // ... insert stuff into map // eg: map.add("something", new MyObject()); String key =

Rails virtual attribute search or sql combined column search

自闭症网瘾萝莉.ら 提交于 2019-11-28 09:14:58
I have a user model with attributes 'first' and 'last' So for example User.first.first #=> "Charlie" User.first.last #=> "Brown" This User model also has a virtual attribute 'full_name' #user.rb def full_name [first,last].join(' ') end def full_name=(name) #don't know what to do with people w/ middle names split = name.split(' ') self.first = split[0] self.last = split[1] end So for example: User.first.full_name = "Charlie Brown" #=> "Charlie Brown" User.first.full_name = "Homer Simpson" #=> "Home Simpson" User.first.save User.first.first #=> "Homer" User.first.last #=> "Simpson" It'd be so

SQL - WHERE Condition on SUM()

时光总嘲笑我的痴心妄想 提交于 2019-11-28 08:53:31
问题 Is it possible to do something like this: SELECT `e`.*, `rt`.`review_id`, (SUM(vt.percent) / COUNT(vt.percent)) AS rating FROM `catalog_product_entity` AS `e` INNER JOIN `rating_option_vote` AS `vt` ON vt.review_id = e.review_id WHERE (rating >= '0') GROUP BY `vt`.`review_id` In particular I would like to put a where condition on the division result value 回答1: This can be accomplished with a HAVING clause: SELECT e.*, rt.review_id, (SUM(vt.percent) / COUNT(vt.percent)) AS rating FROM catalog

Using Action dictionaries instead of switch statements

隐身守侯 提交于 2019-11-28 08:10:23
I'm just reviewing some of my old code (have some spare time), and I noticed a rather lengthy switch statement. Due to gaining new knowledge, I have since refactored it in the following form: private Dictionary<string, Action> createView { get { return new Dictionary<string, Action>() { {"Standard", CreateStudySummaryView}, {"By Group", CreateStudySummaryByGroupView}, {"By Group/Time", CreateViewGroupByHour} }; } } Would you consider this good practise, or is this simply a case of being superflous and unneccessary? I'm keen to ensure new techniques that I learn, are not being clever just for

Angular 2 Pipe under condition

こ雲淡風輕ζ 提交于 2019-11-28 06:42:02
Is it possible in Angular 2 to apply a pipe under condition? I would like to do something like: {{ variable.text | (variable.value ? SomePipe : OtherPipe) }} If not, what is the preferred way to achieve this effect? You need to change the syntax a bit: {{variable.value ? (variable.text | SomePipe) : (variable.text | pipe2)}} Plunker example Since such syntax isn't supported, I think that the only way to do that is to implement another pipe to handle the condition: @Pipe({ name: 'condition' }) export class ConditionPipe { transform(val,conditions) { let condition = conditions[0]; let

Find keyword in text when keyword match certain conditions - C#

杀马特。学长 韩版系。学妹 提交于 2019-11-28 06:38:13
问题 I'm looking for a nice way to do the following: I have an article which has HTML tags in it like anchors and paragraphs and so on. I also have keyword which i need to find in the article and set it as anchor (I have some url to set there). If the keyword does exist in the article it should then match the following TWO conditions BEFORE making it an anchor: It can not be inside any tag. For example, something like <img alt="keyword"> will not be valid/matched. The keyword can't already be

Conditional with statement in Python

别来无恙 提交于 2019-11-28 05:12:32
Is there a way to begin a block of code with a with statement, but conditionally? Something like: if needs_with(): with get_stuff() as gs: # do nearly the same large block of stuff, # involving gs or not, depending on needs_with() To clarify, one scenario would have a block encased in the with statement, while another possibility would be the same block, but not encased (i.e., as if it wasn't indented) Initial experiments of course give indentation errors.. If you want to avoid duplicating code and are using a version of Python prior to 3.7 (when contextlib.nullcontext was introduced) or even

MYSQL select query based on another tables entries

こ雲淡風輕ζ 提交于 2019-11-28 04:36:52
问题 I have stumped on this as I am a total beginner in MySql. Here is a the basic of how the two tables are formed Table 1 id,product_id, product_name Table 2 id,product_id,active Now i know how to do a select statement to query the results from one table but when I have to involve two, I am lost. Not sure if I have to use inner join, left join etc. So how can I return the results of the product_id from table 1 only if in table 2 is active? 回答1: It's pretty simple to join two tables: select t1.*

How to install features depending on the value of property

送分小仙女□ 提交于 2019-11-28 03:51:05
问题 I have a registry key that can be equal to one of two values: special value or null . And two features. When my registry key equals to special value the installer has to install first feature. if registry key is not found by registry search the installer has to install second feature. And if registry key has null value the installer must not install any of these two features. What I'm doing or understanding wrong? If INSTALLLEVEL=5, SPECIALVALUE="special",MYTREAT="1" the first feature must be