I know it was an answer to my question but I can't find it if you can help me with the link (here was the answer on the site). I want to display in Textbox (if I have for example)
Textbox1.Text=3,4,8,17,19,23,24,27,31,32,41,42,48,60,63,66,69,75,78,79
I Want Output:
Textbox2.Lines(0) = 3 - Count Number of interval 1-10
Textbox2.Lines(1) = 2 - Count Number of interval 10-20
Textbox2.Lines(2) = 3 - Count Number of interval 20-30
Textbox2.Lines(3) = 2 - Count Number of interval 30-40
Textbox2.Lines(4) = 3 - Count Number of interval 40-50
Textbox2.Lines(5) = 1 - Count Number of interval 50-60
Textbox2.Lines(6) = 4 - Count Number of interval 60-70
Textbox2.Lines(7) = 3 - Count Number of interval 70-80
Here is some help:
You need to convert your list of numbers from a string(s) into integers:
Dim lst As New List(Of Integer)
For Each item As String In Textbox1.Split(","c)
lst.Add(Convert.ToInt32(Item))
Next
Then you can use LINQ to query for ranges:
Dim count = lst.AsEnumerable().Count(Function(x) x>= 1 AndAlso x < 10)
You need to use AsEnumerable otherwise the standard Count() hides the LINQ extension method Count(Func)
Best of luck!
Related
I am trying to calculate the mean of Interval without selling of a product.
I thought that a good way to get this is:
Count (Days without selling) / Count (Intervals of consecutive days without selling)
Units Sold
0 1
1 4
2 0
3 0
4 0
5 7
6 0
7 0
8 0
9 0
10 1
11 0
In this example I had:
8 days without selling
3 Intervals of consecutive days without selling
So, 8/3 = 2.7 should be my result.
Counting days with No units sold I am using this:
x['Units Sold'] == 0).sum()
However, I don't figured out a good approach to calculate 'Intervals of consecutive days without selling' in a efficient way (considering I will run on multiple products)
Another approach using nunique
s = df["Units Sold"].eq(0)
d = s.sum()
i = s[s].index.to_series().diff().ne(1).cumsum().nunique()
final = d/i # 2.6666666666666665
Using eq, cumsum and diff
First we use eq(0) and sum, to count the amount of days where nothing was sold.
Then we get the cumsum of these days and check wether or not there's a difference between the rows. If this difference is 0, that means there was an interval.
days = x['Units Sold'].eq(0).sum()
intervals = x['Units Sold'].eq(0).cumsum().diff().eq(0)
mask = x['Units Sold'].shift(-1).eq(0)
days / (intervals & mask).sum()
Output
2.6666666666666665
You already knew how to get sum of count of 0, so try this to find number of consective group of 0
s = df['Units Sold'].eq(0)
(s & ~s.shift(fill_value=False)).sum()
Out[567]: 3
You can use:
df.eq(0).sum()/((df.eq(0)&df.shift().ne(0)).sum())
Output:
Units Solds 2.666667
dtype: float64
I'd like to calculate how many different variations of a certain amount of numbers are possible. The number of elements is variable.
Example:
I have 5 elements and each element can vary between 0 and 8. Only the first element is a bit more defined and can only vary between 1 and 8. So far I'd say I have 8*9^4 possibilities. But I have some more conditions. As soon as one of the elements gets zero the next elements should be automatically zero as well.
E.G:
6 5 4 7 8 is ok
6 3 6 8 0 is ok
3 6 7 0 5 is not possible and would turn to 3 6 7 0 0
Would somebody show me how to calculate the amount of combinations for this case and also in general, because I'd like to be able to calculate it also for 4 or 8 or 9 etc. elements. Later on I'd like to calculate this number in VBA to be able give the user a forecast how long my calculations will take.
Since once a 0 is present in the sequence, all remaining numbers in the sequence will also be 0, these are all of the possibilities: (where # below represents any digit from 1 to 8):
##### (accounts for 8^5 combinations)
####0 (accounts for 8^4 combinations)
...
#0000 (accounts for 8^1 combinations)
Therefore, the answer is (in pseudocode):
int sum = 0;
for (int x = 1; x <= 5; x++)
{
sum = sum + 8^x;
}
Or equivalently,
int prod = 0;
for (int x = 1; x <= 5; x++)
{
prod = 8*(prod+1);
}
great thank you.
Sub test()
Dim sum As Single
Dim x As Integer
For x = 1 To 6
sum = sum + 8 ^ x
Next
Debug.Print sum
End Sub
With this code I get exactly 37488. I tried also with e.g. 6 elements and it worked as well. Now I can try to estimate the calculation time
I have found myself into an issue that looked so simple and stupid at the beginning but keeps me struggling to solve it for over 24 hours now.
I have a string (bunch of numbers delimited by |) that I want to be converted into array and then sum some of the array keys depending on the case.
The first issue I have found was the Integer length limitation, I couldn't believe when VBA was unable to return a number higher than 32767 (Then I found longs...). After "solving" that I found that when trying to SUM some 0 values it actually increase my grand total and I can't find any explanation for this.
Below you can see what I have now:
Public Function calcTime(TimeType As String)
Dim jsSting As String
Dim strSplit As Variant
Dim tempTime as Double
jsSting = "100|0|10080|400|0|4320|70|0|1440|30|0|2280|10|0|7400|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|300|0|15855|90|0|1721"
'Split the string by delimiter
strSplit = Split(jsSting, "|")
Select Case UCase(TimeType)
Case "TOTAL"
tempTime = WorksheetFunction.Sum(strSplit(2), strSplit(5), strSplit(8), strSplit(11), strSplit(14), strSplit(17), strSplit(20), strSplit(23), strSplit(26), strSplit(29), strSplit(32), strSplit(35), strSplit(38))
Case "GROUP1" ' Team 1 + Team2
tempTime = WorksheetFunction.Sum(strSplit(2), strSplit(5), strSplit(8), strSplit(11), strSplit(14))
Case "GROUP2" ' Team 1 + Team2 + Team3
tempTime = WorksheetFunction.Sum(strSplit(2), strSplit(5), strSplit(8), strSplit(11), strSplit(14), strSplit(38))
Case "GROUP3" ' Team 5
tempTime = WorksheetFunction.Sum(strSplit(17), strSplit(20), strSplit(23), strSplit(26))
Case "GROUP4" ' Team 2
tempTime = strSplit(14)
Case "GROUP5" ' Team 6
tempTime = WorksheetFunction.Sum(strSplit(29), strSplit(32), strSplit(35))
End Select
Return tempTime
End Function
In this example I have tried to use Excel's SUM function in order to get the desired result but It wasn't a success.
Sticking to the TOTAL case. It sums the following keys - values:
jsString(2) - 10080
jsString(5) - 4320
jsString(8) - 1440
jsString(11) - 2280
jsString(14) - 7400
jsString(17) - 0
jsString(20) - 0
jsString(23) - 0
jsString(26) - 0
jsString(29) - 0
jsString(32) - 0
jsString(35) - 15855
jsString(38) - 0
This gives a total of 41375, however, when I do the sum in VBA I get 43096 and I can't understand why. If I remove from the SUM the values with 0 it returns the correct 41k value.
Hope this makes sense and the answer is simple (I am seriously thinking that I've missed something when assigning the data type).
Thank you in advance for your help !
Did you maybe mean ... ?
Select Case UCase(TimeType)
Case "TOTAL"
tempTime = WorksheetFunction.Sum(strSplit(2), strSplit(5), strSplit(8), strSplit(11), strSplit(14), strSplit(17), strSplit(20), strSplit(23), strSplit(26), strSplit(29), strSplit(32), strSplit(35), strSplit(38)
Anyway, the problem is that you're summing also strSplit(38) which is 1721 even if you wrote in your sample which is equal to 0, exactly the difference between Excel and VBA ;)
Check with a MsgBox strSplit(38) in your code.
strsplit(38) = 1721, which is the difference of 43096 and 41375
I don't know if there is a specific method in VB.Net to calculate the statistical distribution from an array of values like the formula Frequency() in Excel. If not what is the easiest and fastest way of doing the same thing ?
For example I've a DataTable with my values in a Column called "Cement Deviation" :
Column Deviation
0
14
11
2
6
1
16
14
5
21
The bands in which I want to know the frequency of these values are :
From minValue To -50 by Step of 10
From -50 To -10 by Step of 5
From -10 To -5 by Step of 1
From -5 To -1 by Step of 0.5
From -1 To -0.5 by Step of 0.1
From -0.5 To -0.1 by Step of 0.05
From -0.1 To 0.1 by Step of 0.01
From 0.1 To 0.5 by Step of 0.05
From 0.5 To 1 by Step of 0.1
From 1 To 5 by Step of 0.5
From 5 To 10 by Step of 1
From 10 To 50 by Step of 5
From 50 To maxValue by Step of 10
Can someone help me with it?
Thanks
I don't know how you calculate it since my experiences with statistical distribution is limited and you haven't mentioned the way you want to calculate it.
However, this does at least compile:
Dim stat(2) As Integer
For Each row As DataRow In gridView.Rows
Dim cementDeviation = row.Field(Of Int32)("Cement Deviation")
Select Case cementDeviation
Case 0 To 10
stat(0) += 1
Case 10 To 20
stat(1) += 1
End Select
Next
In general there's nothing bad in looping the DataRows to calculate the values. But you should set OPTION STRICT to on, then your code would not compile because row("Cement Deviation") is an object not integer. The good thing is that you are forced to use the correct types which prevents from nasty runtime errors.
Edit Here is an example how you could use dynamic ranges and count each class with LINQ. I have used a DataTable to store the min- and max-values but you could also use a different in-memory collection like List(Of CustomClass) or even better - the database.
You can also simply loop the table but you wanted to see a different approach. I like LINQ since it can reduce complexitiy and increase readability:
The range table with sample data:
Dim rangeTable = New DataTable()
rangeTable.Columns.Add("Min", GetType(Int32))
rangeTable.Columns.Add("Max", GetType(Int32))
For i = 0 To 90 Step 10
rangeTable.Rows.Add(i, i + 10)
Next
A single LINQ query to calculate all occurences for every range even ordered descending:
Dim stats =
From rangeRow In rangeTable
Let min = rangeRow.Field(Of Int32)("Min")
Let max = rangeRow.Field(Of Int32)("Max")
Select StatsInfo = New With {
.Min = min, .Max = max,
.Count = (From devRow In devTable
Let cementDeviation = devRow.Field(Of Int32)("Cement Deviation")
Where cementDeviation >= min AndAlso cementDeviation <= max).Count()
}
Order By StatsInfo.Count Descending
Output the result:
For Each stat In stats
Console.WriteLine("Min: {0} Max: {1} Count: {2}", stat.Min, stat.Max, stat.Count)
Next
Note that i've renamed your DataTable to devTable since gridView is not a good name.
Im looking for a way to loop through variables (eg week01 to week52) and count the number of times the value changes across the them. For example
week01 to week18 may be coded as 1
week19 to week40 may be coded as 4
and week 41 to 52 may be coded as 3
That would be 2 transistions within the data.
How could i go about writing a code that can find me this information? I'm rather new to this and some help to get me in the right direction would be very appreciated.
You can use the DO REPEAT command to loop through variable lists. Below is an example of using this command to create a before date and after date to compare, and increment a count variable whenever these two variables are different.
data list fixed / observation (A1).
begin data
1
2
3
4
5
end data.
*making random data.
vector week(52).
do repeat week = week1 to week52.
compute week = RND(RV.UNIFORM(0.5,4.4)).
end repeat.
execute.
*initialize count to zero.
compute count = 0.
do repeat week_after = week2 to week52 / week_before = week1 to week51.
if week_after <> week_before count = count + 1.
end repeat.
execute.