Could you please help me understand why this statement is incorrect (from a quiz). For some reason I can't see a problem.
if total = 140 then status EQ 'works';
Thanks!
eq is the comparison equals operator, not the assignment equals operator. = performs both roles.
So,
if total eq 140 then status='works';
would be perfectly legal.
Related
I'm trying to highlight a row if the number of blank cells between say, C1 and E1 = 3
and then copy this down for every row.
I've tried using:
=IF(COUNTBLANK($C1:$E1)=3)
But it's not working, can anybody help?
Under conditional formatting, your formula should be the following based on what you've given. The reason is conditional format is trying to see the result as TRUE or False. The IF statement is trying to tell the computer what to do when it's TRUE or FALSE.
COUNTBLANK($C1:$E1)=3
if you want to use IF you will need to do it like this:
=IF(COUNTBLANK($C1:$E1)=3, 1)
I am a bit puzzled why the output is a certain way on the following program.
data a;
input name$ lv ##;
cards;
Frank 1 Joan 2 Sui 3 Burt 4 Kelly . Juan 1
;
data b;
set a;
if lv=.
then expertise='?';
else if lv=1
then expertise='L';
else if lv=2 or 3
then expertise ='M';
else expertise ='H';
run;
proc print data=b;
run;
In the program above, I am expecting that the output for the observation containing Burt has expertise of value H, but for some reason it is M.
I was thinking that the if statement should be lv=(2 or 3) but when I do that now those that I am thinking that the lv= 2 and lv=3 whose expertise should equal M now becomes H as well.
*This is probably explained by the fact that the syntax is inappropriate, and lv is never (2 or 3) /I am not sure why this does not cause an error/ thus the ELSE statement is executed.
I have a feeling that I am not understanding how the ELSE is really working.
However, according to that logic then if the lv=2 part is recognized but the others are not, I am not sure why the lv= 3 and lv=4 does not produce an H.
My goal is to understand why the program runs as if there is no syntax error and why the output is what it is.
Your mistake is here
If lv = 4 or 0
You think this means
If lv = 4 or lv = 0
But it doesn’t. What actually happens is
If (lv = 4) or 0
In SAS 0 is false and any number greater than 1 is true. Not sure about decimals. Anyways. The or condition makes this one false. But in your first example, it makes it always true.
Your syntax is incorrect, but not illegal, so SAS did what you asked for instead of what you wanted. The condition lv=2 or 3 is evaluated from left to right. So lv=2 will result in either 1 or 0 depending whether the value of lv is 2 or not. But both 1 or 3 and 0 or 3 will be true since 3 is always considered true.
What you really want is the in operator.
if missing(lv) then expertise='?';
else if lv=1 then expertise='L';
else if lv in (2 3) then expertise ='M';
else expertise ='H';
Could someone tell me why I can't use a Ternary operator in a mathematical function?
Here's an example:
<cfset test = 1>
<cfdump var="#structKeyExists(Variables, 'test') ? 1 : 0 + 20#">
I would expect the result of this to be 21, however the result actually comes out as 1.
I discovered this on CF10, and when I tested it on Railo it came out the same so I'm wondering: is this a bug or is there a reason I'm not supposed to be able to use a Ternary operator in this context?
The false statement of your ternary doesn't end at the 0, it ends at the end of the line of code. When the check is false, the ternary will return statement right of the :, in your example 0 + 20.
As Beginner has recommended, using brackets will allow you to end the ternary earlier, and continue to manipulate the result. <cfdump var="#(structKeyExists(Variables, 'test') ? 1 : 0) + 20#"> will give you 21 or 20 as expcted.
The following code I wrote was part of an in-class exercise on making "if-statements" in python, which I saved as x.py.
name = raw_input()("Please enter your name:")
print ("Hello", name)
age = int(raw-input("what is your age?"))
print "according to you, your age is", age
print "in a year you will be ", age +1
#if age is >= 21 print "In the state of Massachusetts, you can legally purchase alcoholic drinks"
if age is => 21:
print "In the state of Massachusetts, you can legally purchase alcoholic drinks."
else:
print " something else…"
print "here’s one more thing…"
The problem is that when I try to run the program in Terminal, I get the following error:
File "x.py", line 7
if age is => 21:
^
SyntaxError: invalid syntax
I've tried reentering the line, looking up examples of conditional statements, and scouring stackoverflow for similar questions, but couldn't find any that had an error that was very close to mine. I also can't reach my professor or my TA because of the snowstorm in New England. I'm using Python 2.7.9 on a mac book in v10.9.5. I would greatly appreciate any assistance.
While there are numerous syntax errors in the code you posted as addressed in my comment, the one you asked about is this:
The operator is means "is the exact same object as" and is normally used to compare objects with None.
The operators <, <=, >, >=, ==, != are mathematical comparison operators and are written as is normal in mathematics or symbolic logic, e.g. x < y.
These operators cannot be combined and it makes no sense to do so.
Obviously, there exists a syntax error.
if age >= 21:
print "In the..."
or:
if age is 21:
print "In the..."
So I've been trying to figure out what is wrong with my if-condition, but I am getting nowhere. I am still new to R, so maybe I am not understanding some very basic concept here?
I have a dataframe (dc) to which I appended a column with logical "FALSE". Now I want to change each FALSE into a TRUE based on the values in two columns of dc (dc$Probe and dc$Resp) that I specified using regexpr().
What it does so far is that, for both if-conditions, it changes each FALSE into TRUE regardless of the values in column 5 of dc. When I run the if-conditions seperately, I can see that they seem to be working fine on the OR-part of the condition, meaning the code only generates TRUE when the strings in dc$Probe match one of the strings specified in the OR-part. However, the AND-part seems to be ignored? Thus, when I run the complete code, I get a column with only TRUE, which is not what I want.
Edit: I should get a TRUE only if the string in Probe ends in a certain pattern (as specified in either of the two if conditions I wrote) and if the corresponding value in Resp is a "100" for the patterns specified in my first condition or a "200" for the patterns specified in my second condition. Thus, for strings ending in (sg|s|w1|w3|s1|s2), Resp must be "100" to get a TRUE and for strings ending in (\d\dg|\d\d), Resp must be "200" to get a TRUE. All other cases should be FALSE. For example, if a string ends in s1 and the corresponding value in Resp is 200, the code should return FALSE.
Edit: Some example data:
>dc<-data.frame(Subject=rep("SN",6), item.c=(1:6), Stim=c("XYZc02s03","XYZc01s30","XYZc02s29", "XYZc01s38", "XYZc02s11", "XYZc06w21"), Probe=c("XYzf02s03","XYZf01s30g","XYZf02s29w1","XYZf01s38sg","XYZf02s11s","XYZv06w21s1"), Resp=c(200, 100, 100, 100, 100, 200))
This is my code:
>dc$Resp<-as.character(dc$Resp) #column 5 in dc
dc$Probe<-as.character(dc$Probe)
dc$correct_response <- FALSE
for (i in 1:nrow(dc)) {
if (regexpr("^.*sg$", dc$Probe[i])==1 || regexpr("^.*s$", dc$Probe[i])==1 || regexpr("^.*w1$", dc$Probe[i])==1 || regexpr("^.*w3$", dc$Probe[i])==1 || regexpr("^.*s1$", dc$Probe[i])==1 || regexpr("^.*s2$", dc$Probe[i])==1 && dc[i,5]=="100") {(dc$correct_response[i]<- TRUE)}
if (regexpr("^.*\\d\\dg$", dc$Probe[i])==1 || regexpr("^.*\\d\\d$", dc$Probe[i])==1 && dc[i,5]=="200") {(dc$correct_response[i]<- TRUE)}
}
Is there something wrong with the regular expressions I am using? I checked them with glob2rx() and it seems like they are ok...Is my use of "OR" (||) or/and "AND" (&&) incorrect? How do I implement the AND-part properly? I have also tried the following code for the AND-part, but it didn't change anything:
regexpr("200", dc$Resp[i])==1
I read the R-help on regular expressions and control flow, but I still don't see what I am doing wrong. Consulting other webpages on logical expressions did not help me either.
Please help!
Im wondering if it can all be reduced to the following:
dc<- read.table(header=T,text="Subject item.c Stim Probe Resp
SN 1 XYZc02s03 XYzf02s03 200
SN 2 XYZc01s30 XYZf01s30g 100
SN 3 XYZc02s29 XYZf02s29w1 100
SN 4 XYZc01s38 XYZf01s38sg 100
SN 5 XYZc02s11 XYZf02s11s 100
SN 6 XYZc06w21 XYZv06w21s1 200")
cond1<-regexpr("^.*(sg|s|w1|w3|s1|s2)$", dc$Probe)==1 & dc$Resp==100
cond2<-regexpr("^.*(\\d\\dg|\\d\\d)$", dc$Probe)==1 & dc$Resp==200
dc$correct_response<-cond1|cond2
For one thing, you are missing a logical operator between the 2nd and 3rd clauses of your first if statement.