fill randomly an array in Excel with 0 and 1 without repeating 1 - excel

I want to fill randomly an array in Excel with 0 and 1 but with 1 just one time;
I have tried this formula but it fails:
=IF(COUNTIF($K$43:K43;1)=1;1;0)
My table using :
=RANDBETWEEN(0;1)
0 0 0 0 1 0 1 0 1 0 1 0
Result of my formula:
0 0 0 0 1 1 0 0 0 0 0 0
I want the result of 1 just one time:
0 0 0 0 1 0 0 0 0 0 0 0

This is because COUNTIF is still 1 at stage 0 0 0 0 1 0.
Please use formula as below (eg: if output required on L43)
L43=IF(AND(COUNTIF($K$43:K43,1)=1,L42=0),1,0)
Refer my screenshot.

Related

how to find combinations present in different columns

I have dataset with sample and shop names. I am trying to figure out a way to calculate proportion of shops that sell a combination of samples. For example, sample 12,13 and 22 are available in shop2,3 and 4. Like wise, sample6,7,8,9,10, 16 and 17 is available in shop1.
The dataset i have is very large with 9000 columns and 26 rows. Here what i show is just a small dataset. What i want to do is to figure a way to screen the table for all possible combination of samples present in shops (if >0) and print out in a dictionary, for example sample12_sample13_sample22:[shop2,shop3,shop4] and List out all possible combinations that are available.
Sorry that I could not figure out how to do this, so i do not have any code, right now.
What approach should i use here?
Any help is appreciated.
Thanks!
Name Shop1 Shop2 Shop3 Shop4
Sample1 0 0 0 0
Sample2 0 0 0 0
Sample3 0 0 0 0
Sample4 0 0 0 0
Sample5 0 0 0 0
Sample6 1 0 0 0
Sample7 4 0 0 0
Sample8 12 0 0 0
Sample9 1 0 0 0
Sample10 1 0 0 0
Sample11 0 0 0 0
Sample12 0 5 21 233
Sample13 0 8 36 397
Sample14 0 4 0 0
Sample15 0 0 0 0
Sample16 2 0 0 0
Sample17 17 0 0 0
Sample18 0 0 0 0
Sample19 0 0 0 0
Sample20 0 0 0 0
Sample21 0 0 0 0
Sample22 0 1 20 127
What we can do is melt then we groupby twice
s = df.melt('Name')
s = s[s.value!=0]
s = s.groupby('Name')['variable'].agg([','.join,'count'])
out = s[s['count']>1].reset_index().groupby('join')['Name'].agg(','.join)
out
Out[104]:
join
Shop2,Shop3,Shop4 Sample12,Sample13,Sample22
Name: Name, dtype: object

How to find which combinations have the most frequency?

I have an SPSS data set with 500+ respondents and 18 symptoms that they could have.
Each symptom has its own variable Symptom01 = 1 means they have the symptom 1 Symptom02 = 0 means they dont have the symptom 2 etc etc
What I want to know is what combination of 3 symptoms is more frequent in my data set. For example how many people have symptom 1, 5 and 6; how many people have symptom 1, 2 and 3, etc.
I doesn't mean that they only have those symptoms. Theey could have others. I just want to know which group of 3 symptoms is more frequent in my dataset.
It's a lot of combinations so how would you do this?
Can someone help me?
Please note the macro below uses the variable names Symptom1, Symptom2 etc' instead of "Symptom01", "Symptom02"...
First creating some sample data to work on:
data list list/Symptom1 to Symptom18.
begin data
1 0 0 1 1 0 0 1 1 0 0 0 0 0 1 1 1 1
1 1 1 1 0 1 0 0 1 0 1 1 0 0 1 1 0 0
0 1 1 0 1 1 1 1 1 1 1 0 1 0 0 1 0 0
1 0 0 1 1 0 1 1 1 0 0 0 0 0 0 0 0 0
1 0 1 0 0 1 0 1 0 0 0 0 0 1 0 1 0 0
0 0 0 1 1 1 0 0 0 0 1 0 0 0 1 0 0 1
1 0 1 1 1 1 1 0 1 1 0 0 0 1 1 1 0 1
1 0 0 0 1 0 1 1 0 0 0 1 0 1 0 0 1 0
0 0 1 0 1 0 0 0 0 1 1 0 0 1 0 1 1 1
1 0 0 1 0 0 0 0 0 0 1 1 0 0 1 0 0 0
end data.
Now defining a macro with three loops:
EDIT - this version accounts for repeating combinations of symptoms
define AllCombsOf3 ()
!do !vr1=1 !to 18
!do !vr2=!vr1 !to 18
!do !vr3=!vr2 !to 18
!if (!vr2<>!vr1 !and !vr2<>!vr3) !then
compute !concat("C_",!vr1,"_",!vr2,"_",!vr3)= !concat("Symptom",!vr1)=1 & !concat("Symptom",!vr2)=1 & !concat("Symptom",!vr3)=1 .
!ifend
!doend
!doend
!doend
!enddefine.
Running the macro and displaying wanted results:
AllCombsOf3.
means C_1_2_3 to C_16_17_18.
EDIT 2 - new macro for a four symptom version
define AllCombsOf4 ()
!do !vr1=1 !to 18
!do !vr2=!vr1 !to 18
!do !vr3=!vr2 !to 18
!do !vr4=!vr3 !to 18
!if (!vr2<>!vr1 !and !vr2<>!vr3 !and !vr3<>!vr4) !then
compute !concat("C_",!vr1,"_",!vr2,"_",!vr3,"_",!vr4)=
!concat("Symptom",!vr1)=1 & !concat("Symptom",!vr2)=1 &
!concat("Symptom",!vr3)=1 & !concat("Symptom",!vr4)=1 .
!ifend
!doend !doend !doend !doend
!enddefine.
AllCombsOf4.
means C_1_2_3_4 to C_15_16_17_18.

if cell 1 contains X, then cell 2 should equal W, if cell 1 contains Z, then look at next criteria

I am trying to transform 8 columns of dummy variables into one column of a 8 level rank.
I am trying to do so with this formular:
=IF(OR(A1="1");"1";IF(OR(B1="1");"2";IF(OR(C1="1");"3";IF(OR(D1="1");"4";IF(OR(E1="1");"5";IF(OR(F1="1");"6";IF(OR(G1="1");"7";IF(OR(H1="1");"8";""))))))))
Here is a view on the table col. 1 to 8 is the data and col.9 is what I would like my command to return:
1 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 1
0 0 0 0 1 0 0 0 5
0 1 0 0 0 0 0 0 2
1 0 0 0 0 0 0 0 1
0 0 0 0 0 0 1 0 7
1 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 1
0 0 0 0 1 0 0 0 5
I have used these other stackoverflow questions as inspriration for the structure.
But it does not work, I don't get an error message, but I also don't get the right output.
Anyone who can see where the problem arises? - Would be much appreciated :)
Best wishes,
Mathilde
Use the MATCH() Function:
=MATCH(1,A1:H1,0)
It appears you use ; instead of , for the delimiter. If so use this.
=MATCH(1;A1:H1;0)

How to write a specific data from one Excel sheet to another sheet through script?

Please find the below details,
INPUT OUTPUT
2 8
6 7
0 0
0 0
55 88
0 0
0 0
0 0
0 0
75 94
0 0
0 0
0 0
0 0
30 30
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
45 45
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
14 106
13 13
I have the above data in sheet1, I want to write data sum of the each in column in sheet2.
Are you going to add INPUT(sheet 1) and OUTPUT(sheet 1) and display the result on column 1(sheet 2)? If so, try this;
Private Sub add()
Dim i As Integer
i = 2
Do Until Sheet1.Cells(i, 1) = ""
Sheet2.Cells(i, 1) = Sheet1.Cells(i, 1) + Sheet1.Cells(i, 2)
i = i + 1
Loop
End Sub
I assumed that INPUT is on column 1 and OUTPUT is on column 2.

Transpose row four columns at a time (row to 4-column matrix)

I have data in the following layout (each 'digit' in its own column but all in Row 1 of Excel):
1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7
How might I easily convert this to:
1 2 3 4
5 6 7 8
9 1 2 3
4 5 6 7
I want every four pieces of data to be added, in separate columns, to new rows underneath one another.
If your data starts in A1, please try in A3, copied across to D and down to suit:
=OFFSET($A$1,,COLUMN()+4*(ROW()-2)-5)
This is already answered, but just for fun, here's a version that's non-volatile and doesn't need to be placed in a particular location:
=INDEX($A$1:$P$1,4*(ROW($A$1:$D$4)-1)+COLUMN($A$1:$D$4))
Replace the range $A$1:$P$1 above with whatever range you want to rearrange, and enter as an array formula. (Select the entire 4x4 range, type the formula, and press ctrl-shift-enter when you're finished rather than just enter.) Note that $A$1:$D$4 is a constant, used to obtain the sequential numbers.
One more way - a shorter formula but requiring some setup:
Create a named range, let's call it Matrix1:
1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1
You can put the matrix on another, possibly hidden, worksheet, or you can just use an array constant:
={1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0;0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0;0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0;0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1}
And another, let's call it Matrix2:
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
or:
={1,0,0,0;0,1,0,0;0,0,1,0;0,0,0,1;1,0,0,0;0,1,0,0;0,0,1,0;0,0,0,1;1,0,0,0;0,1,0,0;0,0,1,0;0,0,0,1;1,0,0,0;0,1,0,0;0,0,1,0;0,0,0,1}
Then the formula is just:
=MMULT(Matrix1*$A$2:$P$2,Matrix2)
Again, select the entire 4x4 range and enter as an array formula.
You could, of course, enter the array constants into the formula directly, but that gets unwieldy:
=MMULT({1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0;0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0;0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0;0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1}*$A$2:$P$2,{1,0,0,0;0,1,0,0;0,0,1,0;0,0,0,1;1,0,0,0;0,1,0,0;0,0,1,0;0,0,0,1;1,0,0,0;0,1,0,0;0,0,1,0;0,0,0,1;1,0,0,0;0,1,0,0;0,0,1,0;0,0,0,1})
I found this formula much easier to deal with.
=INDEX($A:$A,ROW(A1)*4-4+COLUMN(A1))
For details:
https://www.extendoffice.com/documents/excel/3360-excel-transpose-every-5-rows.html

Resources