Sumproduct nested inside countifs - excel-formula

Am trying hard to crack this excel formula but need help. Basically I need count of all "p" & "f" on from different column ranges+rows (Status 1-4), if one the corresponding row value (Site) is "CH". Here is what the data looks like.
Status 1 Status 2 Status 3 Status 4 Site
P f n t CH
P f n t DL
P P P P BD
f f P P CR
f f f f CH
P P f f DL
P P P P BD
t t t t CR
t t t t CH
P f n t DL
P f P f BD
P f P f CR
P P P P CH
Any quick help will be much appreciated. I tried formula - =Countif(E2:E14,"CH",SUMPRODUCT(COUNTIF(A2:D14,{"p","f"}))), of course I knew that's wrong!

You need to combine several countifs that have OR conditions and sum them all up, for example like this:
=SUM(COUNTIFS($E$2:$E$14,"CH",A2:A14,{"p","f"}),COUNTIFS($E$2:$E$14,"CH",B2:B14,{"p","f"}),COUNTIFS($E$2:$E$14,"CH",C2:C14,{"p","f"}),COUNTIFS($E$2:$E$14,"CH",D2:D14,{"p","f"}))
You don't specify the expected result, but according to your written logic, it should be 10 and the formula returns just that.

Here is another approach. As much as I like to avoid INDIRECT, sometimes it is necessary.
=SUMPRODUCT(COUNTIFS(E2:E999, "CH", INDIRECT(ADDRESS(2, ROW(1:4), 4, 1)&":"&ADDRESS(999, ROW(1:4), 4, 1)), {"f","p"}))

Related

pivot table help in excel to get a tree map

i need help in resolving a time consuming job
Input example
part number
where used
a
d
a
l
b
e
c
f
d
g
d
m
g
h
f
i
h
j
l
k
Result tree i need:
a
d
g
h
j
a
l
k
i am using pivot tabe to get this, but i cant arrive at that, kindly provide your guidance to make it

python3 - create list of strings elliptically

EDIT: I do not want your code! just help me think of a nice way to do it :)
I have a string with 25 characters:
ABCDEFGHIKLMNOPQRSTUVWXYZ
and I want to create a matrix5*5 given a position and a direction,
position can be one of the corners and direction can be clock or counter clock.
so if I gave this arguments:
create((0,0), clock)
I want to recive:
["ABCDE", "QRSTF", "PYZUG", "OXWVH", "NMLKI"]
and I could then print it and recieve:
A B C D E
Q R S T F
P Y Z U G
O X W V H
N M L K I

Split a single row of data (dat file) into multiple columns

I want to split a row of data into multiple columns like
a.dat
A B C D E F G H I J K L M N O P Q R S T U
into
b.dat
A B C D E F G
H I J K L M N
O P Q R S T U
I have tried using the pr function
pr -ts" " --columns 7 --across a.dat > b.dat
But it doesn't work, b.dat is the same as a.dat
I like fold for these thingies:
$ fold -w 14 file
A B C D E F G
H I J K L M N
O P Q R S T U
With -w you set the width you desire to have.
Although xargs is more useful if you want to split based on number of fields instead of characters:
$ xargs -n 7 < file
A B C D E F G
H I J K L M N
O P Q R S T U
Regarding your attempt in pr: I don't really know why it is not working, although from some examples I see it doesn't look like the tool for such job.

(Haskell) Parse error in pattern after Currying

I am getting a parse error after I changed this:
h :: ([Int],Int,[Int])->[[Int]]
h ([],k,x) =[[]]
h(y:[],k,x) = [x++k:[y]]
h(y:xs,k,x)= [x++k:y:xs]++h(xs,k,x++[y])
to this: at line 3
h :: [Int]->Int->[Int]->[[Int]]
h [] k x =[[]]
h (y:[]) k x = [x++k:[y]]
h y:xs k x = [x++k:y:xs]++h(xs,k,x++[y])
There are two problems with this line:
h y:xs k x = [x++k:y:xs]++h(xs,k,x++[y])
^^^^ ^^^^^^^^^^^^^
(1) (2)
You need parens around this pattern - (y:xs)
Unlike in other languages like C, Java, C#, etc, in Haskell you don't use commas to separate parameters to functions.

Haskell take sum recursively

Hello I want to take a sum of functions call in Haskel but I cannot figure out what I am doing wrong. To be more specific, I have a function f(a,b,c)=a+b+c and I want to take an int like this:
x=Sum( from i=0 to i=c) f(1,1,i)
so far I have written this, but it doesn't even compile. Can you help me?
f a b c = a+b+c
my_sum f a b c+1 =f a b c+1 + my_sum f a b c
I get parse error in pattern my_sum
eg for my_sum f 1 1 5 the result would be f(1,1,5)+f(1,1,4)+f(1,1,3)+f(1,1,2)+f(1,1,1)
I dont want to use lists
n+k patterns are bad
Your code:
my_sum f a b c+1 =f a b c+1 + my_sum f a b c
includes a pattern in the form c+1 which A) should have parentheses B) Needs a base case (I assume you want to stop when c == 0) and C) is a syntactic form that has been removed from the language.
Instead, explicitly subtract 1 from c when you want and be sure to handle the base case:
my_sum f a b 0 = f a b 0
my_sum f a b n = f a b n + my_sum f a b (n-1)
This also has a memory leak meaning it will build up a large computation in the form f1 + (f a b n' + (f a b n'' + (f a b n''' + (.... You can handle the leak by using an accumulator or a higher level function and optimization at compile-time.
A cleaner Solution
List comprehension strikes me as the most reasonable solution here:
sum [f a b i | i <- [0..c] ]
The sum of the function f applied to arugments a, b and finally i where i ranges from 0 to c inclusively.
You can't have the c+1 on the left side of a definition. Since you're just summing, it doesn't matter if you count up from 0 to c or count down from c to 0, so you could instead do
my_sum f a b 0 = f a b 0
my_sum f a b c = f a b c + my_sum f a b (c - 1)
Then you could use it as
> let g x y z = x + y + z
> my_sum g 0 0 10
55
Some more detail on why your code failed to compile: Whenever you have a pattern on the left side of a definition, such as
fib 0 = 1
fib 1 = 1
fib n = fib (n - 1) + fib (n - 2)
You can only match on constructors, names (like n or c), and literals (which are essentially constructors for the basic types). The function + is not a constructor, it is a function belonging to the Num typeclass, so therefore you can not pattern match on it. You may be confused from seeing list pattern matching before because it uses an operator:
myListSum [] = 0
myListSum (x:xs) = x + myListSum xs
but in fact, : is the Cons constructor for lists, and [] is the empty list constructor. You can think of the list type defined as
data [a] = [] | a : [a]
Or, if you were to replace all the symbols with words
data List a = Empty | Cons a (List a)
although its a bit different in reality since there's more that goes into defining lists, but that's the basic idea. This means that a pattern like
f [] = ...
f (x:xs) = ...
Is equivalent to
f Empty = ...
f (Cons x xs) = ...
just with more convenient syntax.
However, Int can be though of as a very large ADT defined as
data Int = -2147483648 | -2147483647 | ... | -1 | 0 | 1 | ... | 2147483646 | 2147483647
where each number itself is a different constructor. Then you can match on any individual number, but not anything like (x + 1) or (x * 2), because + and * are not constructors, just regular functions. (Note: Int is not actually defined this way because that would be really inefficient, it's defined at a more primitive level)
You can get from list formulations to the non-list, recursive formulations, with manual inlining and fusing of the functions in play:
{-# LANGUAGE BangPatterns #-}
import Data.List
f a b c = a+b+c
g f a b c = sum . map (f a b) $ [0..c]
= foldl' (\ !x y -> x + f a b y) 0 $ enumFromTo 0 c
= h 0 0 where
h !acc i | i > c = acc
| otherwise = h (acc + f a b i) (i+1)
Strictness annotations prevent uncontrolled build-up of thunks and stack overflow for big values of c.

Resources