MS Access 3 conditions in a query? - iif

I have a total column that ranges from -7 to 7. I want to recode them in a grand total variable so everything under 0 is now 0, 1 stays 1, 2 stays 2, and 3 and everything above just stays 3. I am stuck on how to include a third condition.
This is the code I have which makes everything above 3 a 3, and keeps 1's and 2's as is. But I do not know how to make all negative numbers become a 0.
GrandTotal: IIf([total]>=3,3,[total])

chained condition, try this:
GrandTotal: IIf([total]<0;0;IIf([total]>=3;3;[total]))

Related

Need to subtract value of one column from all others; Can't figure out mistake in this line?

I need to take columns 3 to 14-1 and subtract value of column 14 from all of these:
labdata_snus_var[,3:ncol(labdata_snus_var)-1] <- labdata_snus_var[,14] - labdata_snus_var[,3:ncol(labdata_snus_var)-1]
You need to put () around ncol(...)-1.
labdata_snus_var[,3:(ncol(labdata_snus_var)-1)] <- labdata_snus_var[,14] - labdata_snus_var[,3:(ncol(labdata_snus_var)-1)]
In your example 3:ncol(labdata_snus_var)-1 you get a vector from 3 to 14 and each value will be decreased by one. The result is a vector from 2 to 13.
The brackets around (ncol(labdata_snus_var)-1) means you substract one just from ncol(labdata_snus_var). So you get a vector from 3 to 13.

R circular "linked" list: adding +1 to last index brings you to first index

I'm trying to implement movement through four points, while recording which points I visit. Think of it as a square. I can move from corner to corner or diagonally.
If you 'unwrap' the square you get a straight line with four points, which can be thought of as 1-2-3-4- where after 4 it goes back to 1. So if I'm at point 2 I can move to 1 and 3 directly or 4 diagonally. I'd implement that as 2-1 / 2+1 for corner-to-corner or 2+/-2 for diagonally. The problem occurs when I'm at 2 and will try to subtract 2 where I'll end up outside of the list.
The thought I've had is that if I could somehow translate my "out of bounds" numbers to in bounds this would be solved. One solution is hard coding that:
0=4
-1=3
5=1
6=2
but I'm pretty sure there is a better way to do this, however I can't seem to find it.
It seems to me all you want is modular arithmetic (bless the lord for math)
magicFun <- function (x) x %% 4
Here is a simple test run
> magicFun(0:6)
[1] 0 1 2 3 0 1 2
Addendum
It's more about math but the reason it works for negatives is that in Z/nZ ("the world where n is equal to 0") n is "identified" to 0.
This means you can add n as many times as you wish to a given number without changing it's "value".
Also, by convention the numbers in Z/nZ are listed as {0, 1, ..., n-1}.
So suppose n = 4 and x = -6, by the above x = x + 2*4 = 2.

Find all combinations 6 numbers using only 0,1 and 2

Is there a simple way of finding all the combinations of 6 digits using only 0, 1 and 2?
So it starts like 000000 and finishes 222222
I have looked online but all i can find is the formula for finding how many there are but i need a list of all of them
If there is a code in R that will be even better
It is not completely neccessary but if there is a way to create a list where the 1st and 4th digit sum to a maximum of 2, 2nd and 5th digit sum to a maximum of 2 and 3rd and 6th digit sum to a maximum of 2
Thankyou
You can do:
do.call(paste0, expand.grid(rep(list(0:2), 6)))
Adding a rev in there gives a different order that might feel more natural:
do.call(paste0, rev(expand.grid(rep(list(0:2), 6))))
I will only give you a hint for your new (added) question as I am now worried I might be doing your homework. expand.grid returns a data.frame. With a little work on it, you can probably extract the subset of rows that only matter to you.

Depth First Search Example

I have a Depth First Search example for a practice exam and I have asked another question regarding it and I think I have some of the concepts of it down...
I just want to confirm the results I got are correct or not, and if you can guide me what I did wrong and how I can fix it.
Here is a picture of it:
http://i.imgur.com/FdKxIUw.png
The results I got for DFI in order from 0 - 8 are :
1 5 7 6 2 3 4 8 9
For parent, I got confused because it starts at 0, the parent of 0 is 4? and the parent of 4 is 5? I am confused with parent, so if someone can clarify that my results for DFI are correct / wrong , and also help me with Parent, I would GREATLY appreciate it. This is all practice review for an exam.
Thanks everyone.
If you are doing a DFS on this graph starting from 0, then one example of the order you will visit the vertices is
{0, 4, 5, 6, 1, 3, 7, 8, 2}. The parent of each of the vertices from the given order (as represented by (vertex, parent) pairs) would be {(0,-)(4,0),(5,4),(6,0),(1,6),(3,1),(7,3),(8,3),(2,6)}
Hope this helps!
DFI looks ok to me.
The DFI tells you the order in which the nodes are found. Does not necessarily give you the information on parent.
The node you start from should not have any parent, since that is the root of your DFS tree. Have a look at the DFI, 0 is visited before any other node. That is why it has 1 in DFI.
Then next is 4, which has DFI 2. Since 4 is found from 0, its parent is 0. Then you found 5 from 4, so 5 should have parent 4. Then you traced back to 0. And so on for the rest of the graph.

Math: Five numbers with unique sums

So I need a way to figure out how to get 5 numbers, and when you add any 2 of them, it will result in a sum that you can only get by adding those specific two numbers.
Here's an example of what I'm talking about, but with 3 numbers:
1
3
5
1 + 3 = 4
1 + 5 = 6
3 + 5 = 8
Adding any two of those numbers will end up with a unique sum that cannot be found by adding any other pair of the numbers. I need to do this, but with 5 different numbers. And if you have a method of figuring out how to do this with any amount of numbers, sharing that would be appreciated as well.
Thank you
1, 10, 100, 10000, 100000 gives you five numbers like you desire.
In general, 1, 10, 100, 1000, ..., 10^k where k is the number of numbers that you need.
And even more general, you can say b^0, b^1, ..., b^k, where b >= 2. Note that you have the special property that not only are all the pairwise sums unique, but all the subset sums are unique (just look at representations in base b).
The set {1, 2, 5, 11, 21} also works.
You can start with a set of two or three elements that fit that property (any addition operation on two elements from the set {1,2,5} gives you an unique sum) and only include the next number being considered if additions of current elements and this new element also give you unique sums.
An example run-through:
Suppose our starting set S is S={1,2,5}. Let U be the set of all sums between two elements in S.
Elements in S give us unique sums 1+2=3, 1+5=6, 2+5=7, so U={3,6,7}.
Consider adding 11 to this set. We need to check that 1+11, 2+11, and 5+11 all give us sums that are not seen in U and are all unique among themselves.
1+11=12, 2+11=13, 5+11=17.
Since 12, 13, and 17 are all unique sums among themselves, and are not found in U, we can update S and U to be:
S1 = {1,2,5,11}
U1 = {3,6,7,12,13,17}.
You can do the same procedure for 21, and you should (hopefully) get:
S2 = {1,2,5,11,21}
U2 = {3,6,7,12,13,17,22,23,26,32}.
If all you need is a quick set though, the solution that Jason posted is a lot faster to produce.
1
2
4
8
16
1
3
9
27
81
suggests x ^ n where n is a member of a subset of Natural numbers

Resources