I am trying to do an if statement to test whether the variable m is equal to game1 or game2 and if it is show it as a two move game and if its game3 or game4 show it as a one move game.
game1 = Image[]
game2 = Image[]
game3 = Image[]
game4 = Image[]
The above 4 variables are assigned to 4 different images.
m := RandomChoice[{game1, game2, game3, game4}];
If[m === game1 || game2 , InputString["This is a two move game"],
InputString["This is a one move game"]]
This is totally failing. the 4 game variables are assigned to images and we need to show the image and have a input box pop up
This is another alternative I came up with that also failed.
m := RandomChoice[{game1, game2, game3, game4}];
If[m == game1 || game2 , InputString["This is a two move game"]];
If[m == game3 || game4 , InputString["This is a one move game"]]
Any help would be much appreciated.
You can't use this syntax: m === game1 || game2
For example instead of 1 == 2 || 3 you should use 1 == 2 || 1 == 3
However, you are using m multiple times, and each time it is used it will change. So you need to fix m, e.g.
m := RandomChoice[{game1, game2, game3, game4}];
a = m;
If[a == game1 || a == game2 , InputString["This is a two move game"]];
If[a == game3 || a == game4 , InputString["This is a one move game"]]
moves01 = Thread[{game3, game4} -> "one move game"]
moves02 = Thread[{game1, game2} -> "two move game"]
moves = Association#Catenate[{moves01, moves02}]
m := RandomChoice[{game1, game2, game3, game4}];
moves[m]
Related
I have a data source in tableau that looks something similar to this:
SKU Backup_Storage
A 5
A 1
B 2
B 3
C 1
D 0
I'd like to create a calculated field in tableau that performs a SUM calculation IF the SKU column contains the string 'A' or 'D' , and to perform an AVERAGE calculation if the SKU column contains the letters 'C' or 'B'
This is what I am doing:
IF CONTAINS(ATTR([SKU]),'A') or
CONTAINS(ATTR([SKU]),'D')
THEN SUM([Backup_Storage])
ELSEIF CONTAINS(ATTR([SKU]),'B') or
CONTAINS(ATTR([SKU]),'C')
THEN AVG([Backup_Storage])
END
UPDATE - desired output would be:
SKU BACKUP
A, D 6 (This is the SUM OF A and D)
B, C 2 (This is the AVG of B and C)
The calculation above shows as valid, however, I see NULLS in my data source table.
Any suggestion is appreciated.
I have named the calculated field:
SKU_FILTER_CALCULATION
Basically, IF THEN ELSE condition works when one test that is either TRUE/FALSE. Your specified condition is not a proper use case of IF THEN ELSE because SKUs can take all possible values. See it like this..
your data
SKU Backup_Storage
A 5
A 1
B 2
B 3
C 1
D 0
Let's name your calc field as CF, then CF will take value A in first row and will output SUM(5) = 5. For second row it will output sum(1) = 1, for third and onward rows it will output as avg(2) = 2, avg(3) = 3, avg(1) and sum(0) respectively. all these values just equals [Backup_storage] only and I'm sure that this you're not trying to get.
If instead you are trying to get sum(5,1,0) + avg(2,3,1) (obviously i have assumed + here) which equals 8 i.e. one single value for whole dataset, please proceed with this calculated field..
SUM(IF CONTAINS([SKU], 'A') OR CONTAINS([SKU], 'D')
THEN [Backup storage] END)
+
AVG(IF CONTAINS([SKU], 'B') OR CONTAINS([SKU], 'C')
THEN [Backup storage] END)
This will return an 8 when put to view
Needless to say, if you want any other operator instead of + you have to change that in CF accordingly
As per your edited post, I suggest a different methodology. Create diff groups where you want to perform different aggregations
Step-1 Create groups on SKU field. I have named this group as SKUG
Step-2 create a calculated field CF as
SUM(ZN(IF CONTAINS([SKU], 'A') OR CONTAINS([SKU], 'D')
THEN [Backup storage] END))
+
AVG(ZN(IF CONTAINS([SKU], 'B') OR CONTAINS([SKU], 'C')
THEN [Backup storage] END))
Step-3 get your desired view
Good Luck
I need some help with creating a new variable. I feel like the egen function is what I need to use but I can't figure it out.
I have 3 variables for cancer treatment - radiotherapy, chemotherapy and surgery - given with the number of times each patient has received each treatment.
I would like to create a new "Treatment" variable where 1 = radiotherapy, 2 = chemotherapy, 3 = surgery, 4 = combination (with 1 or more for any of the above 3), 5 = none
You should always show code you have tried and give example data. See https://stackoverflow.com/help/mcve for guidance.
Assume variables radio chemo surgery with values 0 or positive.
gen treatment = 5
replace treatment = 1 if radio & !chemo & !surgery
replace treatment = 2 if chemo & !radio & !surgery
replace treatment = 3 if surgery & !chemo & !radio
replace treatment - 4 if ((surgery > 0) + (radio > 0) + (chemo > 0)) > 1
using the facts that non-zero is true and its negation is false. See this FAQ
Another way to do it:
gen treatment = 5
replace treatment = 1 if radio
replace treatment = cond(treatment == 1, 4, 2) if chemo
replace treatment = cond(inlist(treatment, 1, 2), 4, 3) if surgery
In similar circumstances I would code your none category 0, not 5. That's likely to yield more sensible graphs and tables.
Code not tested.
I am trying to show navigation in R plot. The current status in time one or (t1) is set as val$currentstatus and next status in (t2) wants to be shown in the graph based on the action that the user choose from the checkbook. then I want to draw a line to show this path. The code that I wrote is as following
output$navigation<-renderPlot({
#initial state of X and Y
if(is.element("Within", vals$currentstatus))
x <- 1
y <- 2
if(is.element("Out", vals$currentstatus)) {
x <- 1
y <- 1
}
action<-c(input$action1,input$action2)
x<-1:4
y<-1:2
rewards<-matrix(rep(0,8),nrow=2)
rewards[1,4]<- -1
rewards[2,4]<- 1
values<-rewards#initial Values
states<-expand.grid(x=x,y=y)
#Transition probability
transition <- list( input$action1= c("within" = 0.8, "out" = .2),
input$action2= c("within" = 0.3, "out" = .7))
# The value of an action (e.g. move toward benchmark )
action.values <- list(input$action1 = c("x" = 1, "y" = 0),
input$action1 = c("x" = 1, "y" = 0))
# act() function serves to move the agent to go to new position
act <- function(action, state) {
action.value <- action.values[[action]]
new.state <- state
#
if(state["x"] == 4 && state["y"] == 1 || (state["x"] == 4 && state["y"] == 2))
return(state)
#
new.x = state["x"] + action.value["x"]
new.y=if(transition["within">"out"]){state["y"==2]}
if(transition["within"<"out"]){state["y"==1]}
}
plot(x, y, xaxt='n',yaxt='n',cex=10,pch=19,
col=ifelse(y==1,"red","green"),ylab="status",xlab="period",
xlim=c(0,4), ylim=c(0,3))
axis(1,at=1:4,labels=c("t1","t2","t3","t4"))
axis(2,at=1:2,labels=c("out bench","within bench"))
if next position is within bench it should be green and connect to the previous state and if it is out of bench should be red and connect to previous state. Also I want to see the name of chosen action on the connection line between two states.Moreover I want to know how can I update the new position and use it for calculating next state in next period (t3) and so force.Similar to the following graph:
I am using this statement
if ((pm && pn) || (pm == false && pn == false))
it is supposed to return true only if both pm and pn are true or if both are false. But this is also returning true if only only first one (pm) is true.
So now it is acting like this:
0 0 = 1
0 1 = 0
1 0 = 1
1 1 = 1
but I need it to work like this:
0 0 = 1
0 1 = 0
1 0 = 0
1 1 = 1
can you tell me where am I making mistake?
What you want is simply:
if (pm == pn)
You are checking if pm is true twice. You also need to check if both are the same, not whether they are both true. So,
if ((pm == pn)
^^ ^^
pm && pm
should be
pm && pn
^
The whole expression can be simplified to
pm == pn
if the variables already have bool type.
Why not try xor?
if (!(pm ^ pn)) { /*...*/ }
Or simply equal?
if (pm == pn) { /*...*/ }
if ((pm && pm) || (pm == false && pn == false))
it is supposed to return true only if both pm and pn are true or if both are false. But this is also returning true if only only first one (pm) is true.
Because you made a typo. You meant pm && pn.
Instead just write if (pm == pn), which is equivalent along as the only semantic values are indeed true and false for both variables.
Plus, consider making your variable names clearer and more distinct.
Note that operator precedence has nothing to do with this.
Since the question's title asks about precedence, note that || has lower precedence than &&. So the two sets of inner parentheses are redundant, and the original expression is just a longer way of saying
if (pm && pm || pm == false && pn == false)
Now, fixing the obvious typo:
if (pm && pn || pm == false && pn == false)
Removing the unneeded explicit comparisons:
if (pm && pn || !pm && !pn)
And, finally, a less obvious transformation, which others have suggested:
if (pm == pn)
I have problem to make regExp for search panel
&& (item.get('prodAddDate') >= dateStartValue.format("Y-m-d"))
&& (item.get('prodAddDate') <= dateEndValue.format("Y-m-d"));
I'm not sure if this is task for refExp but don't have any other idea
Problem is that when I'm don't fill field with date I can't filtr data with other conditions
I was trying something like this but don't working
&& ((item.get('prodAddDate') >= dateStartValue.format("Y-m-d")) || (new RegExp(dateStartValue)).test(item.get('prodAddDate'))) &&
((item.get('prodAddDate') <= dateEndValue.format("Y-m-d")) || (new RegExp(dateEndValue)).test(item.get('prodAddDate')));
Not entirely sure what you're trying to do, but here are some pieces of information that I hope can help you to solve your problem.
Comparison operators priority
The && operator has priority over ||, which means that:
A || B && C || D
Is equivalent to:
A || (B && C) || D
Not to:
(A || B) && (C || D)
Date comparison
You can compare Date objects directly:
// example data
var d1 = new Date('2012-12-12'),
d2 = new Date('2012-12-12'),
d3 = new Date('2013-01-01');
And get the result you expect with <, >, <=, and >=:
// logical results
d1 < d3 // true
d1 < d2 // false
d2 > d3 // false
d1 <= d2 // true
d1 => d2 // true
But not with equality comparison ==:
d1 == d2 // false
// yet...
d1 <= d2 && d1 => d2 // true
Conclusion: to test if one date is before or after another one, direct comparison is OK. But, in order to test if two dates are identical, use string comparisons:
// is this the same day?
d1.format('Y-m-d') === d2.format('Y-m-d') // true
// is this the same day and hour?
d1.format('Y-m-d H:i:s') === d2.format('Y-m-d H:i:s') // true
Ext.isEmpty()
Ext.isEmpty returns true for: null, undefined and empty strings '' only.
Ext.isEmpty(null) // true
Ext.isEmpty(undefined) // true
Ext.isEmpty('') // true
Ext.isEmpty(' ') // false
Ext.isEmpty(false) // false
// etc.
That may be useful to address your empty field case:
...
&& (Ext.isEmpty(item.get('prodAddDate') || item.get('prodAddDate') >= dateStartValue)
&& (Ext.isEmpty(item.get('prodAddDate') || item.get('prodAddDate') <= dateEndValue)
Finally
What's this obsession about regex? They're useful for complex string testing/extracting, but that's all. You should probably forget about them for a while, dude ;p