H= 1..24;
s.t. ElectBattery{h in H}: ES[h]-ES[h-1]-P2S[h]*Efi['ESt']+PGEN['ESt',h]==0;
error: ES[0] out of domain
One way:
Make sure ES[h] is defined over 0..24 (instead of 1..24)
Fix ES[0] to a known value by adding a constraint ES[0]=10 (say).
If you want a steady-state solution, use:
ES[0] = ES[24]
Instead of this, you can also use an if-then-else construct like:
ES[h] - (if h=1 then ES[24] else ES[h-1]) - ..
Related
I want to create a new column based on a range of columns(GAP1-GAP5). I wanted to use something like this:
IF FIND(GAP1-GAP5,'New Start') THEN FILTER_NewStart=1
but got an error so had to use this:
IF FIND(GAP1,'New Start') OR FIND(GAP2,'New Start') OR FIND(GAP3,'New Start')
OR FIND(GAP4,'New Start') OR FIND(GAP5,'New Start') THEN FILTER_NewStart=1;
Do I need to use a loop or can I use a function for a range of columns to achieve this?
Try concatenating them and using find on the variable instead.
if(find(cat(of GAP1-GAP5), 'New Start') ) then FILTER_NewStart = 1;
Sounds like you want to use the WHICHC() function to find the first variable in a list of variables that equals a specific value. If none have it then the result is zero.
FILTER_NewStart=0 ne whichc('New Start',of GAP1-GAP5);
FIND() is for locating a specific substring in a longer string and so cannot work on many variables at once.
#Tom whichc() is a good idea, whereas it doesn't work if GAP1-GAP5 just contain but not equal to 'New Start'. find() would be a better anwser in this situation.
Can someone explain to me why only the first IF ELSE statement in my code works? I am trying to combine multiple variables into one.
DATA BCMasterSet2;
SET BCMasterSet;
drop PositiveLymphNodes1;
if PositiveLymphNodes1 = "." then PositiveLymphNodes =
put(PositiveLymphNodes2, 2.);
else PositiveLymphNodes = PositiveLymphNodes1;
if PositiveLymphNodes2 = "." then new_posLymph = put(PositiveLymphNodes,
2.);
else new_posLymph = PositiveLymphNodes2;
RUN;
Here is a nice screenshot of what the incorrect output looks like:OUTPUT
Thanks!
Hard to say without seeing all of your data, but I have a suspicion: is positivelymphnodes1 character or numeric? Is it ever actually equal to "."?
If you are trying to say "if PositiveLymphNodes1 is missing", then you can say that this way:
if missing(positivelymphnodes1) then ...
You can also do the same thing using coalesce or coalescec (the latter is character, the former numeric, in its return value). It chooses the first nonmissing argument. - so if the first argument is missing, it chooses the second.
positiveLymphNodes = coalescec(PositiveLymphNodes1, put(positiveLymphNodes2,2.));
new_posLymph = coalescec(positiveLymphNodes2, put(positiveLymphNodes,2.));
I would be curious why you're using put only in one place and not the other - use it in both or neither, I would suggest.
I am trying to change the name of a variable to be called in r on the fly.
For example, dataframe trades_long_final has many columns "prob_choice1" and "prob_choice2", ... "prob_choiceN" and "col1", "col2", ... "colN".
I want to change the value of each on the fly.
For example,
trades_long_final$"prob_choice1"[1] = 10 and
trades_long_final$"prob_choice2"[1] = 10
works
but not
trades_long_final$gsub("1","2","prob_choice1")[1] = 10
as a way to call trades_long_final$"prob_choice2"[1] by substituting the 1 in prob_choice1 with a 2 because I get the error
Error: attempt to apply non-function
I need this to work because I need to loop over the columns using something like trades_long_final$gsub("i","2","prob_choicei")[1] in a loop for all i.
Thank you so much for your help. It must be a command I don't know how to use...
Instead of using $, you can use [ to change the variable name and assign the value in one line.
trades_long_final[,gsub("1","2","prob_choice1")][1] <- 10
But, it is not clear why you need to do this. Simply
trades_long_final[1, "prob_choice2"] <- 10
would be easier. From the description, "prob_choice2" is already a column in the dataset. So, it is confusing.
data
set.seed(24)
trades_long_final <- data.frame(prob_choice1 =runif(10),
prob_choice2=rnorm(10), col1=rnorm(10,10), col2=rnorm(10,30))
as akrun said, the way to do it was to use [ so that the way I did it was:
for (k in 1:numtrades){
trades_long_final[[paste("prob_choice", k, sep="")]] =
{some complex procedure...}
}
Given the following table:
Table: Comedians
=================
Id First Middle Last
--- ------- -------- -------
1 Bob NULL Sagat
2 Jerry Kal Seinfeld
I want to make the following prepared query:
SELECT * FROM Comedians WHERE Middle=?
work for all cases. It currently does not work for the case where I pass NULL via sqlite3_bind_null. I realize that the query to actually search for NULL values uses IS NULL, but that would mean that I cannot use the prepared query for all cases. I would actually have to change the query depending on the input, which largely defeats the purpose of the prepared query. How do I do this? Thanks!
You can use the IS operator instead of =.
SELECT * FROM Comedians WHERE Middle IS ?
Nothing matches = NULL. The only way to check that is with IS NULL.
You can do a variety of things, but the straight forward one is...
WHERE
middle = ?
OR (middle IS NULL and ? IS NULL)
If there is a value you know NEVER appears, you can change that to...
WHERE
COALESCE(middle, '-') = COALESCE(?, '-')
But you need a value that literally NEVER appears. Also, it obfuscates the use of indexes, but the OR version can often suck as well (I don't know how well SQLite treats it).
All things equal, I recommend the first version.
NULL is not a value, but an attribute of a field. Instead use
SELECT * FROM Comedians WHERE Middle IS NULL
If you want match everything on NULL
SELECT * FROM Comedians WHERE Middle=IfNull(?, Middle)
if want match none on NULL
SELECT * FROM Comedians WHERE Middle=IfNull(?, 'DUMMY'+Middle)
See this answer: https://stackoverflow.com/a/799406/30225
I want to use the parameter place holder - e.g. ?1 - with the % wild cards. that is, something like: "u.name LIKE %?1%" (though this throws an error). The docs have the following two examples:
1.
// Example - $qb->expr()->like('u.firstname', $qb->expr()->literal('Gui%'))
public function like($x, $y); // Returns Expr\Comparison instance
I do not like this as there is no protection against code injection.
2.
// $qb instanceof QueryBuilder
// example8: QueryBuilder port of: "SELECT u FROM User u WHERE u.id = ?1 OR u.nickname LIKE ?2 ORDER BY u.surname DESC" using QueryBuilder helper methods
$qb->select(array('u')) // string 'u' is converted to array internally
->from('User', 'u')
->where($qb->expr()->orx(
$qb->expr()->eq('u.id', '?1'),
$qb->expr()->like('u.nickname', '?2')
))
->orderBy('u.surname', 'ASC'));
I do not like this because I need to search for terms within the object's properties - that is, I need the wild cards on either side.
When binding parameters to queries, DQL pretty much works exactly like PDO (which is what Doctrine2 uses under the hood).
So when using the LIKE statement, PDO treats both the keyword and the % wildcards as a single token. You cannot add the wildcards next to the placeholder. You must append them to the string when you bind the params.
$qb->expr()->like('u.nickname', '?2')
$qb->getQuery()->setParameter(2, '%' . $value . '%');
See this comment in the PHP manual.
The selected answer is wrong. It works, but it is not secure.
You should escape the term that you insert between the percentage signs:
->setParameter(2, '%'.addcslashes($value, '%_').'%')
The percentage sign '%' and the symbol underscore '_' are interpreted as wildcards by LIKE. If they're not escaped properly, an attacker might construct arbirtarily complex queries that can cause a denial of service attack. Also, it might be possible for the attacker to get search results he is not supposed to get. A more detailed description of attack scenarios can be found here: https://stackoverflow.com/a/7893670/623685