Multiple IF statements between number ranges - if-statement

I'm trying to set up a formula with multiple IF statements between number ranges but I'm seeing the error:
Formula parse error
Here is the forumula:
=IF((AND(A2>0,A2<500),"Less than 500",
If(AND(A2>=500,A2<1000),"Between 500 and 1000"),
If(AND(A2>=1000,A2<1500),"Between 1000 and 1500"),
If(AND(A2>=1500,A2<2000),"Between 1500 and 2000")))

It's a little tricky because of the nested IFs but here is my answer (confirmed in Google Spreadsheets):
=IF(AND(A2>=0, A2<500), "Less than 500",
IF(AND(A2>=500, A2<1000), "Between 500 and 1000",
IF(AND(A2>=1000, A2<1500), "Between 1000 and 1500",
IF(AND(A2>=1500, A2<2000), "Between 1500 and 2000", "Undefined"))))

I suggest using vlookup function to get the nearest match.
Step 1
Prepare data range and name it: 'numberRange':
Select the range. Go to menu: Data → Named ranges... → define the new named range.
Step 2
Use this simple formula:
=VLOOKUP(A2,numberRange,2)
This way you can ommit errors, and easily correct the result.

standalone one cell solution based on VLOOKUP
US syntax:
=IFERROR(ARRAYFORMULA(IF(LEN(A2:A),
IF(A2:A>2000, "More than 2000",VLOOKUP(A2:A,
{{(TRANSPOSE({{{0; "Less than 500"},
{500; "Between 500 and 1000"}},
{{1000; "Between 1000 and 1500"},
{1500; "Between 1500 and 2000"}}}))}}, 2)),)), )
EU syntax:
=IFERROR(ARRAYFORMULA(IF(LEN(A2:A);
IF(A2:A>2000; "More than 2000";VLOOKUP(A2:A;
{{(TRANSPOSE({{{0; "Less than 500"}\
{500; "Between 500 and 1000"}}\
{{1000; "Between 1000 and 1500"}\
{1500; "Between 1500 and 2000"}}}))}}; 2));)); )
alternatives: https://webapps.stackexchange.com/questions/123729/

For multiple conditions that are sequential, Google sheets provides the more efficient and legible IFS formula:
=IFS(
A2<500, "Less than 500",
A2<1000, "Between 500 and 1000",
A2<1500, "Between 1000 and 1500",
A2<2000, "Between 1500 and 2000" )
You could also add a condition for cases less than 0 or greater than 2000, but this is basically what you asked.

Shorter than accepted A, easily extensible and addresses 0 and below:
=if(or(A2<=0,A2>2000),"?",if(A2<500,"Less than 500","Between "&500*int(A2/500)&" and "&500*(int(A2/500)+1)))

Related

Simplify google sheet formula "SUM / INDEX / MATCH"

I am trying to use google sheet to create a roster formula, to sum up the duty hour per week using INDEX/MATCH/SUM.
But it's too long, is there any way to simplify the formula?
Also, I realize "MATCH" cannot recognize blank cell (N20), can that be fixed too?
=IFERROR(SUM(INDEX($O$12:$O$20,MATCH(D17,$N$12:$N$20,0)),INDEX($O$12:$O$20,MATCH(E17,$N$12:$N$20,0)),INDEX($O$12:$O$20,AND(F17,$N$12:$N$20,0)),INDEX($O$12:$O$20,MATCH(G17,$N$12:$N$20,0)),INDEX($O$12:$O$20,MATCH(H17,$N$12:$N$20,0)),INDEX($O$12:$O$20,MATCH(I17,$N$12:$N$20,0)),INDEX($O$12:$O$20,MATCH(J17,$N$12:$N$20,0))),"Err")
try:
=ARRAYFORMULA(MMULT(IFERROR(REGEXREPLACE(UPPER(B6:H14), "^"&TEXTJOIN("$|^", 1, L1:L10)&"$",
VLOOKUP(REGEXEXTRACT(UPPER(B6:H14), "^"&TEXTJOIN("$|^", 1, L1:L10)&"$"), L1:M10, 2*
SIGN(ROW(A6:A14)), 0)&""), UPPER(B6:H14))*1, TRANSPOSE(COLUMN(B:H))^0))
Franco, since your post says your end goal is to "sum up the duty hour per week," I take that to mean all you need in the end is a single number.
Try this (which will give you total hours for your block that runs B6:H22:
=ArrayFormula(SUM(COUNTIF(B6:H22,$L$1:$L$8)*$M$1:$M$8))
If you need to see the breakdown per code, you can use this:
=ArrayFormula({$L$1:$L$8,COUNTIF(B6:H22,$L$1:$L$8)*$M$1:$M$8})
Just replace "B6:H22" with the reference of each calendar block to get the sum or the breakdown for other weeks.

Is there way to count the number of items in a row based on a criteria in one column?

So I am trying to figure out the formula for this. I've been trying to use COUNTIF and COUNTIFS functions but to no avail. I'm trying to count the number of occurrences for 30 minutes. It should show 1.
=COUNTIF(D7:D17,R7,E7:N10,"")
...where D7:D17 is the range in the first column and R7 is "30 minutes". E7:N10 is that range from 1-10 column. I get the error:
"Array arguments to COUNTIFS are of different size." Then I tried E10:N10 and still got the same error.)
try:
=ARRAYFORMULA(SUM(IF(A2:A="30 minutes",
MMULT(IF(B2:K<>"", 1, 0), TRANSPOSE(COLUMN(B:K))^0), )))

how to use parenthesis or accomplish complex cell rules upon stated logic

Setup:
Logic:
If a person worked 6 days he needs to rest 2 days otherwise cell O3=1
Cell O3 is empty if a rule is not violated
days are up to 31
those 6 days needs to be in a row followed by 2 days in a row
point is to check all 26 possible "chunks" of cells
So far I got this:
=IF( OR( IF( AND( SUM(B3:G3)=6; SUM(H3:I3)=0); SUM(C3:H3)=6; SUM(I3:J3)=0) ); 1; ); 0; 1;)
Thoughts?
This answer requires a slight change to you data, but seems to work. It requires replacing the blank cells (non-working days) with zero. The join creates a single string ('1111100111111011111101111110100'). Then the string is searched for occurrences of '11111101'. It counts those occurrences. If count is zero, blank else 'Violation'.
=if((LEN(join("",B3:AF3))-LEN(SUBSTITUTE(join("",B3:AF3),"11111101",)))/LEN("11111101")=0,"","Violation")
Copy the formula down.
#Michael has a good point. I added an OR to the above formula with handles both situations. The one above and 7 or more 1s in a row:
=if(or((LEN(join("",B3:AF3))-LEN(SUBSTITUTE(join("",B3:AF3),"1111111",)))/LEN("1111111")>=1,LEN(join("",B3:AF3))-LEN(SUBSTITUTE(join("",B3:AF3),"11111101",)))/LEN("11111101")=0,"","Violation")

Make =IF Function Output Numbers For "Scoring": Google Sheets

I'm am exploring methods of giving scores to different datapoints within a dataset. These points come from a mix of numbers and text string attributes looking for certain characteristics, e.g. if Col. A contains more than X number of "|", then give it a 1. If not, it gets a 0 for that category. I also have some that give the point when the value is >X.
I have been trying to do this with =IF, for example, =IF([sheet] = [Text], "1","0").
I can get it to give me 1 or 0, but I am unable to get a point total with sum.
I have tried changing the formatting of the text to both "number", "plain text", and have left it as automatic, but I can't get it to sum. Thoughts? Is there maybe a better way to do this?
FWIW - I'm trying to score based on about 12 factors.
Best,
Alex
The issue here might be that you're having the cell evaluate to either the string "0" or the string "1" rather than the number 0 or the number 1. That would explain why you're seeing the right things but the math isn't coming out right - the cell contents look like numbers, but they're really text, which the summation would then ignore.
One option would be to drop the quotation marks and write something like this:
=IF(condition, 1, 0)
This has the condition evaluate to 1 if it's true and 0 if it's false.
Alternatively, you could write something like this:
=(condition) * 1
This will take the boolean TRUE or FALSE returned by condition and convert it to either the numeric value 1 (true) or the numeric value 0 (false).

Count number of words in one list that appear in a string

I have a unique set of words in a character vector (that have been 'stemmed') and I want to know how many of them appear in a string.
Here's what I have so far:
library(RTextTools)
string <- "Players Information donation link controller support years fame glory addition champion Steer leader gang ghosts life Power Pellets tables gobble ghost"
wordstofind <- c("player","fame","field","donat")
# I created a stemmed list of the string
string.stem <- colnames(create_matrix(string, stemWords = T, removeStopwords = F))
I know the next step probably involves grepl("\\bword\\b,value") or some usage of regex, but I'm not sure what the fastest option is in this case.
Here are my criteria:
I have to do this many times, so it being as fast as possible is a concern.
It should match the entire word ("es" shouldn't match "test").
Any push in the right direction would be great.
Well, I never work with huge datasets, so time is never of the essence, but given the data you've provided this will give you a count of how many words exactly match something in the string. Might be a good starting point.
sum(wordstofind %in% unlist(strsplit(string, " ")))
> sum(wordstofind %in% unlist(strsplit(string, " ")))
[1] 1
Edit Using the stems to get the proper 3 matches, thanks to #Anthony Bissel:
sum(wordstofind %in% unlist(string.stem))
> sum(wordstofind %in% unlist(string.stem))
[1] 3
Take a look at stringr by Hadley Wickham. You are probably looking for the function str_count.
There certainly might be a faster option, but this works:
length(wordstofind) - length(setdiff(wordstofind, string.stem)) # 3
But it looks like Andrew Taylor's answer is faster:
`microbenchmark(sum(wordstofind %in% unlist(string.stem)), length(wordstofind) - length(setdiff(wordstofind, string.stem)))
Unit: microseconds
expr min lq mean median uq max neval
sum(wordstofind %in% unlist(string.stem)) 4.016 4.909 6.55562 5.355 5.801 37.485 100
length(wordstofind) - length(setdiff(wordstofind, string.stem)) 16.511 16.958 21.85303 17.404 18.296 81.218 100`