If Cell Starts with String from a List Return Number - excel

What I am looking for is either a formula or Macro/code that will allow me to do the following:
I have a list of the beginning of Postcodes, for example, "AB31, AB32, AB33, AB34", I need something that will return a value of 313 if the start of a cell contains a value from this list I have, and a value of 74 if it does not start with a value from that list.
Now the list is quite long, theres probably 100+ values in there. If there weren't so many values in the list I could use something like
=IF(LEFT(K1,4)="AB51","313","72")
The Postcode will be in the K column, and the full list I need it to search through is
AB31
AB32
AB33
AB34
AB35
AB36
AB37
AB38
AB40
AB41
AB42
AB43
AB44
AB45
AB46
AB47
AB48
AB49
AB50
AB51
AB52
AB53
AB54
AB55
AB56
BT
GY
HS
IM
IV1
IV2
IV3
IV4
IV5
IV6
IV7
IV8
IV9
IV10
IV11
IV12
IV13
IV14
IV15
IV16
IV17
IV18
IV19
IV20
IV21
IV22
IV23
IV24
IV25
IV26
IV27
IV28
IV30
IV31
IV32
IV36
IV40
IV41
IV42
IV43
IV44
IV45
IV46
IV47
IV48
IV49
IV51
IV52
IV53
IV54
IV55
IV56
IV63
J3
KA27
KA28
KW1
KW2
KW3
KW4
KW5
KW6
KW7
KW8
KW9
KW10
KW11
KW12
KW13
KW14
KW15
KW16
KW17
PA20
PA21
PA22
PA23
PA24
PA25
PA26
PA27
PA28
PA29
PA30
PA31
PA32
PA33
PA34
PA35
PA36
PA37
PA38
PA41
PA42
PA43
PA44
PA45
PA46
PA47
PA48
PA49
PA60
PA61
PA62
PA63
PA64
PA65
PA66
PA67
PA68
PA69
PA70
PA71
PA72
PA73
PA74
PA75
PA76
PA77
PA78
PH4
PH5
PH6
PH7
PH8
PH9
PH10
PH11
PH12
PH13
PH14
PH15
PH16
PH17
PH18
PH19
PH20
PH21
PH22
PH23
PH24
PH25
PH26
PH27
PH28
PH29
PH30
PH31
PH32
PH33
PH34
PH35
PH36
PH37
PH38
PH39
PH40
PH41
PH42
PH43
PH44
PH49
PH50
TR21
TR22
TR23
TR24
TR25
ZE
So if any Postcode in column K begins with any of those values, I would like the number 313 returning, if not the number 72 returning.
Any help would be much appreciated.
Thanks!

You should give example on how your sheets look like or else it is very difficult to answer.
what I will do
Put the list on a Column
Use combination of IF and Vlookup to loop through the list.
=IF(VLOOKUP(LEFT(K1, 4),A:A,1,0)=LEFT(K1, 4),313,72)
Column E Column F Column G
Post Code Result Formula
AB31456 313 =IF(VLOOKUP(LEFT(E2,4),A:A,1,0)=LEFT(E2,4),313,72)
KZ12398 72 =IF(VLOOKUP(LEFT(E2,4),A:A,1,0)=LEFT(E2,4),313,72)

Related

What is the simplest way to complete a function on every row of a large table?

so I want to do a fisher exact test (one sided) on every row of a 3000+ row table with a format matching the below example
gene
sample_alt
sample_ref
population_alt
population_ref
One
4
556
770
37000
Two
5
555
771
36999
Three
6
554
772
36998
I would ideally like to make another column of the table equivalent to
[(4+556)!(4+770)!(770+37000)!(556+37000)!]/[4!(556!)770!(37000!)(4+556+770+37000)!]
for the first row of data, and so on and so forth for each row of the table.
I know how to do a fisher test in R for simple 2x2 tables, but I wouldn't know how I would apply the fisher.test() function to each row of a large table. I also can't use an excel formula because the numbers get so big with the factorials that they reach excel's digit limit and result in a #NUM error. What's the best way to simply complete this? Thanks in advance!
Beginning with a tab-delimited text file on desktop (table.txt) with the same format as shown in the stem question
if(!require(psych)){install.packages("psych")}
multiFisher = function(file="Desktop/table.txt", saveit=TRUE,
outfile="Desktop/table.csv", progress=T,
verbose=FALSE, digits=3, ... )
{
require(psych)
Data = read.table(file, skip=1, header=F,
col.names=c("Gene", "MD", "WTD", "MC", "WTC"), ...)
if(verbose){print(str(Data))}
Data$Fisher.p = NA
Data$phi = NA
Data$OR1 = format(0.123, nsmall=3)
Data$OR2 = NA
if(progress){cat("\n")}
for(i in 1:length(Data$Gene)){
Matrix = matrix(c(Data$WTC[i],Data$MC[i],Data$WTD[i],Data$MD[i]), nrow=2)
Fisher = fisher.test(Matrix, alternative = 'greater')
Data$Fisher.p[i] = signif(Fisher$p.value, digits=digits)
Data$phi[i] = phi(Matrix, digits=digits)
OR1 = (Data$WTC[i]*Data$MD[i])/(Data$MC[i]*Data$WTD[i])
OR2 = 1 / OR1
Data$OR1[i] = format(signif(OR1, digits=digits), nsmall=3)
Data$OR2[i] = signif(OR2, digits=digits)
if(progress) {cat(".")}
}
if(progress){cat("\n"); cat("\n")}
if(saveit){write.csv(Data, outfile)}
return(Data)
}
multiFisher()

Failing to use sumproduct on date ranges with multiple conditions [Python]

From replacement data table (below on the image), I am trying to incorporate the solbox product replace in time series data format(above on the image). I need to extract out the number of consumers per day from the information.
What I need to find out:
On a specific date, which number of solbox product was active
On a specific date, which number of solbox product (which was a consumer) was active
I have used this line of code in excel but cannot implement this on python properly.
=SUMPRODUCT((Record_Solbox_Replacement!$O$2:$O$1367 = "consumer") * (A475>=Record_Solbox_Replacement!$L$2:$L$1367)*(A475<Record_Solbox_Replacement!$M$2:$M$1367))
I tried in python -
timebase_df['date'] = pd.date_range(start = replace_table_df['solbox_started'].min(), end = replace_table_df['solbox_started'].max(), freq = frequency)
timebase_df['date_unix'] = timebase_df['date'].astype(np.int64) // 10**9
timebase_df['no_of_solboxes'] = ((timebase_df['date_unix']>=replace_table_df['started'].to_numpy()) & (timebase_df['date_unix'] < replace_table_df['ended'].to_numpy() & replace_table_df['customer_type'] == 'customer']))
ERROR:
~\Anaconda3\Anaconda4\lib\site-packages\pandas\core\ops\array_ops.py in comparison_op(left, right, op)
232 # The ambiguous case is object-dtype. See GH#27803
233 if len(lvalues) != len(rvalues):
--> 234 raise ValueError("Lengths must match to compare")
235
236 if should_extension_dispatch(lvalues, rvalues):
ValueError: Lengths must match to compare
Can someone help me please? I can explain in comment section if I have missed something.

Calculate the average of Spearman correlation

I have 2 columns A and B which contain the Spearman's correlation values as follows:
0.127272727 -0.260606061
-0.090909091 -0.224242424
0.345454545 0.745454545
0.478787879 0.660606061
-0.345454545 -0.333333333
0.151515152 -0.127272727
0.478787879 0.660606061
-0.321212121 -0.284848485
0.284848485 0.515151515
0.36969697 -0.139393939
-0.284848485 0.272727273
How can I calculate the average of those correlation values in these 2 columns in Excel or Matlab ? I found a close answer in this link : https://stats.stackexchange.com/questions/8019/averaging-correlation-values
The main point is we can not use mean or average in this case, as explained in the link. They proposed a nice way to do that, but I dont know how to implement it in Excel or Matlab.
Following the second answer of the link you provided, which is the most general case, you can calculate the average Spearman's rho in Matlab as follows:
M = [0.127272727 -0.260606061;
-0.090909091 -0.224242424;
0.345454545 0.745454545;
0.478787879 0.660606061;
-0.345454545 -0.333333333;
0.151515152 -0.127272727;
0.478787879 0.660606061;
-0.321212121 -0.284848485;
0.284848485 0.515151515;
0.36969697 -0.139393939;
-0.284848485 0.272727273];
z = atanh(M);
meanRho = tanh(mean(z));
As you can see it gives mean values of
meanRho =
0.1165 0.1796
whereas the simple mean is quite close:
mean(M)
ans =
0.1085 0.1350
Edit: more information on Fisher's transformation here.
In MATLAB, define a matrix with these values and use mean function as follows:
%define a matrix M
M = [0.127272727 -0.260606061;
-0.090909091 -0.224242424;
0.345454545 0.745454545;
0.478787879 0.660606061;
-0.345454545 -0.333333333;
0.151515152 -0.127272727;
0.478787879 0.660606061;
-0.321212121 -0.284848485;
0.284848485 0.515151515;
0.36969697 -0.139393939;
-0.284848485 0.272727273];
%calculates the mean of each column
meanVals = mean(M);
Result
meanVals =
0.1085 0.1350
It is also possible to calculate the total meanm and the mean of each row as follows:
meanVals = mean(M); %total mean
meanVals = mean(M,2); %mean of each row

Why doesn't countif returning correct results?

I am attempting to count different values in an Excel column, whose values are taken from another sheet in the same file.
i.e. the value of D2 is "=VLOOKUP(A2, Journals!B:E, 2, FALSE)", which returns 3.057.
I have written the following functions:
=COUNTIF($D$2:$D$201, ">10")
=COUNTIFS($D$2:$D$201, "<10",$D$2:$D$201, ">=5")
=COUNTIFS($D$2:$D$201, ">=1",$D$2:$D$201, "<5")
=COUNTIF($D$2:$D$201, "<1")
=COUNTIF($D$1:$D$201, #N/A)
The total count should be 200 values, but I am only counting 53 for an unknown reason, and different values for each cell.
For example, there are 26 values larger than 10, but I get 7 (if I change the value to >10* I get 133, which is totally bizarre).
Does anyone know why that is?
Here is the data:
3.057
5.228
0.879
9.423
8.490
4.587
8.405
5.008
9.202
11.329
5.992
37.369
2.352
0.821
6.661
4.409
38.138
2.400
6.585
3.634
18.393
3.040
4.391
2.934
3.154
13.334
5.463
2.561
3.920
3.052
4.534
3.043
13.779
2.134
7.760
4.258
6.975
5.531
3.148
8.005
2.332
7.870
4.575
44.002
2.787
5.924
1.737
1.053
8.303
3.544
4.820
4.197
6.059
6.035
3.867
13.038
2.466
3.794
4.426
11.351
9.269
1.206
3.784
3.632
2.989
5.089
2.043
2.948
2.914
2.118
#N/A
5.621
5.912
1.991
4.638
7.760
4.706
2.587
8.580
4.317
10.794
5.181
7.003
21.407
2.341
3.559
20.982
11.847
5.812
5.766
5.985
28.710
2.049
2.291
4.358
5.538
3.912
17.759
2.042
1.387
3.859
3.570
8.166
4.681
1.627
5.771
4.919
2.320
3.635
1.847
1.434
12.384
1.858
4.487
2.776
3.169
0.841
4.037
14.921
2.796
10.125
4.314
3.849
7.932
3.093
5.101
2.319
1.413
6.280
1.923
5.677
#N/A
4.165
1.833
3.408
2.183
5.557
1.542
2.382
3.792
1.647
2.093
15.064
1.942
5.196
5.840
3.792
2.857
3.064
4.985
8.934
4.218
3.603
4.453
1.731
3.636
4.667
2.656
8.285
1.680
12.485
10.581
3.943
3.016
5.610
6.615
9.463
3.519
1.113
29.298
2.754
5.634
16.240
5.301
2.760
7.242
3.782
2.081
10.383
59.558
8.668
5.040
3.014
4.606
5.874
3.818
4.580
6.915
6.991
3.842
3.184
2.000
3.540
11.000
1.674
4.470
4.465
3.905
3.464
4.803
So far I have attempted playing with * to widen the range and copy-paste value to make sure the problem isn't somehow made by the values coming from a different sheet, but nothing works.
Also the column is defined as numbers with 3 digits after .
Ok, following Jordan's comment, the functions in D were changed to:
=value(vlookup())
This solved the issue and allowed the countif to work correctly.
I'm not sure why this worked and simply formatting the column to number didn't, but there you have it.
Thanx #Jordan
I replicated your code and got
26
53
116
3
2
Are your numbers formatted as numbers? What about locale (decimal dot or comma)? Formula arg separator (comma or semicolon?)
=COUNTIF($D$2:D201; ">10")
=COUNTIFS($D$2:$D$201; "<10";$D$2:$D$201; ">=5")

excel / VBA - Data Distribution

Warm Morning.
I would like to assign user name to a sheet which have the data to work by them.
Let say I have 350 Case study tickets to share to 22 student.
how to i do it??? Need to share the case ticket equally or near for every one.
for this case should be atlease 15 for every one or for some one may reach 16 /17 not a problem.
Kindly help me in this .............
Sheet 1
Stud Name
AA
BB
CC
DD
EE
FF
GG
HH
II
JJ
KK
LL
MM
NN
OO
PP
QQ
RR
SS
TT
UU
VV
Sheet 2
Case ID's
D08457P
D24023
B03890
B03894
B00475
D02275
D02484
D12559
D04526
D04527
D49651
D49661
D49662
B81263
D04445
W00163P
D12034
D02427
E14005
D19521-3
D19521-4
D00690
D10646
D08206
D10244
B03642-1
D05404P
D05404-1
D05405-2
D10224
D03752
B06770
B26687
D26724
D26736
D11102
D06034
D02734
B10780
D10777
B02516
B04854
B02530
C06558
B81647
B81648
B02717
C81384
C81396
D08465
D14773
C14524-3
D14550
D14559
D09113
D09122
D83314
D83319
B25438
D25435
D81397
B14806
D14807
D14823
D21950
C35978
D38750
D38753P
D87178
D18595
D85838
B00227
B00228
D02002
D81016
D04880
D02272
D03502
D02448
D03199
D02169
D03047
B06467
D09275
D09287
C81515
D11495
D02856
D81144
D06682
D17530
D17562
B16804
D16813
D16822
D05129
W00209
B38786-2
B38853
B38860
B38867P
B38867-1
B38875
B38883
B38889
B83523
B83524
D38870
D10938
B04572
D23029-1
D23065
B06968
B24466
D24468P
B82288
B82289
B82290
D13064
D13065
D04040-1
C13958
D14002
B17932
B17940
D17944
D81223
D02629
C01451
D01449
B11591
D11585
D11588
D11597
D11598
B04746
B07800
D07798
B26740
D84601
C10256
D81889
D84666
D84676
D03678
B11165
D11162
D23757
B33828
B33843
D33845
D33851
D33855
D33857
D82528
B02984
D11052
D00090
D02511
D01666
D01671
D05085
D10059
D05723
D14007
D14025
C01302
C03382
D00008
C13464
B05936
B02765
D81911
C11907
D02411
D02531
D13879
D13883
D03630
B19277
B82771
D49639
D20119
D08779
D08780P
D08785
B14819
D08018
D08028P
D08038
D08039
C81463
C86333
D13907
D13908
B36126
C36082
D36105
D36111
D36128
D87174
D02009
D00930
B04013
D04008
B06939
D06928
D06935
D06936
D06940
B03253
D04443
D03050
D05187
D04776
B10005
D09998
B03491
B12270
D12229
D12244
D06672
D05759
D02715
D47895
C26222
D13338
D37177
D37185
D07653P
D07653-4
D04883
D02532
D81966
E06977P
B08182
E16920
D03827
D04349
B04659P
B04659-1
D36203
D36206
D18071
D82132
D82133
D83345
D83346
D83348
D11709
D10254
D14098
D84649
D84672
D03649
D33825
D08626
D17695
D05549
D05552
D06360
D06149
D10453
D10511
D10517
D10534
D18791
B00353
C02068
D04258
D22169
D07259
D19377
D19387
C83047
B12385
D19694P
D04436
B02730
D05268
D17514
D17528
D17529
D17531
D17542
D04565
B07643
D37168
D10280
D09196
D09210
D14367
D06604
D04418P
C03913
D04152P
B36141
D02780
B12833
D02488
B10644
C05708
D11565
D85529
D03957
D05216
B00053
D11607
C81980
D08735
D06295
B08173
B84677
B04674
D00560P
B07903-3
B08038-1
C00219
D81550
D18488
D11990-2
D12044
D02608
D19585-2
D19599-2
D81128
B09349
D09014
D03584
D02832
D06170
C81758
B06689
D02789
D02355
B27537
D27539
D27540
B25430P
C03889
D06472
B38823
B81421
B24482
B82699
D24469
D03256
D17939
D11690
D16924
D81468
C00381
D00283
D00373
C07313
D04722
D04723P
D06147
D03779
D03824
D03825
D03652
D04801
C02639
D00053
C81933
D03908
D01293
D15980
D10252
D04265
D06981
D04150
C00025
B03251
D81044
Sheet 3
Case ID's
D08457P
D24023
B03890
B03894
B00475
D02275
D02484
D12559
D04526
D04527
D49651
D49661
D49662
B81263
D04445
W00163P
D12034
D02427
E14005
D19521-3
D19521-4
D00690
D10646
D08206
D10244
B03642-1
D05404P
D05404-1
D05405-2
D10224
D03752
B06770
B26687
D26724
D26736
D11102
D06034
D02734
B10780
D10777
B02516
B04854
B02530
C06558
B81647
B81648
B02717
C81384
C81396
D08465
D14773
C14524-3
D14550
D14559
D09113
D09122
D83314
D83319
B25438
D25435
D81397
B14806
D14807
D14823
D21950
C35978
D38750
D38753P
D87178
D18595
D85838
B00227
B00228
D02002
D81016
D04880
D02272
D03502
D02448
D03199
D02169
D03047
B06467
D09275
D09287
C81515
D11495
D02856
D81144
D06682
D17530
D17562
B16804
D16813
D16822
D05129
W00209
B38786-2
B38853
B38860
B38867P
B38867-1
B38875
B38883
B38889
B83523
B83524
D38870
D10938
B04572
D23029-1
D23065
B06968
B24466
D24468P
B82288
B82289
B82290
D13064
D13065
D04040-1
C13958
D14002
B17932
B17940
D17944
D81223
D02629
C01451
D01449
B11591
D11585
D11588
D11597
D11598
B04746
B07800
D07798
B26740
D84601
C10256
D81889
D84666
D84676
D03678
B11165
D11162
D23757
B33828
B33843
D33845
D33851
D33855
D33857
D82528
B02984
D11052
D00090
D02511
D01666
D01671
D05085
D10059
D05723
D14007
D14025
C01302
C03382
D00008
C13464
B05936
B02765
D81911
C11907
D02411
D02531
D13879
D13883
D03630
B19277
B82771
D49639
D20119
D08779
D08780P
D08785
B14819
D08018
D08028P
D08038
D08039
C81463
C86333
D13907
D13908
B36126
C36082
D36105
D36111
D36128
D87174
D02009
D00930
B04013
D04008
B06939
D06928
D06935
D06936
D06940
B03253
D04443
D03050
D05187
D04776
B10005
D09998
B03491
B12270
D12229
D12244
D06672
D05759
D02715
D47895
C26222
D13338
D37177
D37185
D07653P
D07653-4
D04883
D02532
D81966
E06977P
B08182
E16920
D03827
D04349
B04659P
B04659-1
D36203
D36206
D18071
D82132
D82133
D83345
D83346
D83348
D11709
D10254
D14098
D84649
D84672
D03649
D33825
D08626
D17695
D05549
D05552
D06360
D06149
D10453
D10511
D10517
D10534
D18791
B00353
C02068
D04258
D22169
D07259
D19377
D19387
C83047
B12385
D19694P
D04436
B02730
D05268
D17514
D17528
D17529
D17531
D17542
D04565
B07643
D37168
D10280
D09196
D09210
D14367
D06604
D04418P
C03913
D04152P
B36141
D02780
B12833
D02488
B10644
C05708
D11565
D85529
D03957
D05216
B00053
D11607
C81980
D08735
D06295
B08173
B84677
B04674
D00560P
B07903-3
B08038-1
C00219
D81550
D18488
D11990-2
D12044
D02608
D19585-2
D19599-2
D81128
B09349
D09014
D03584
D02832
D06170
C81758
B06689
D02789
D02355
B27537
D27539
D27540
B25430P
C03889
D06472
B38823
B81421
B24482
B82699
D24469
D03256
D17939
D11690
D16924
D81468
C00381
D00283
D00373
C07313
D04722
D04723P
D06147
D03779
D03824
D03825
D03652
D04801
C02639
D00053
C81933
D03908
D01293
D15980
D10252
D04265
D06981
D04150
C00025
B03251
D81044
You don't need any VBA for this. Say both lists are in column A of their respective sheets. In B2 of Sheet1 enter:
=OFFSET(Sheet2!$A$2,COLUMNS($A:A)-1+(ROW()-2)*16,0)
and copy this across through cell Q2
then copy cells B2 through Q2 downwards:
Assign any leftovers by hand.
Note:
Any single column can be mapped into a 2D table of arbitrary dimensions, either in column order or row order. The same is true for a single row of data.
In a similar fashion any 2D table can be mapped into a single column or single row.

Resources