Creating many rows with binomial data

kajO

New Member
Joined
Oct 23, 2020
Messages
6
Office Version
  1. 365
Platform
  1. Windows
Hi All,

I have a file that consists of thousands of rows of beehive data. It is currently set up so that each apiary owner has their own row that includes the number of hives they own and what percentage didn't survive. I need this data to be binomial. For instance, in the first row of the attached image, I would have 8 rows with ones for lost and 54 rows with zeros for alive. I need to find a way to avoid doing this all manually as it would likely take hundreds of hours but I don't have a clue where to start.
 

Attachments

  • Untitled.png
    Untitled.png
    19.5 KB · Views: 9

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Your problem description is not clear to me. Can you post, using XL2BB, a sample of your data and include manual entries of where the 0's and 1's should appear?

Link to XL2BB
 
Upvote 0
Here is a sample of the data:

BIP_LMS_0708_1819_COREDATA(v.3.0)(n72543v91)(2019-09-23).xlsx
BVBWBXBY
1WinterAtRiskWinterLostWinterAliveWinterLOSS
26285412.90322581
33659027524.65753425
46008052013.33333333
514007013305
62102118910
BIP_LMS_0708_1819_COREDATA(v.3.



And here is the first row converted to binomial. Only the alive/lost column would matter at this point

Book1
ABCDE
10Alive/lostWinterAtRiskWinterLostWinterAliveWinterLOSS
1116285412.9
1216285412.9
1316285412.9
1416285412.9
1516285412.9
1616285412.9
1716285412.9
1816285412.9
1906285412.9
2006285412.9
2106285412.9
2206285412.9
2306285412.9
2406285412.9
2506285412.9
2606285412.9
2706285412.9
2806285412.9
2906285412.9
3006285412.9
3106285412.9
3206285412.9
3306285412.9
3406285412.9
3506285412.9
3606285412.9
3706285412.9
3806285412.9
3906285412.9
4006285412.9
4106285412.9
4206285412.9
4306285412.9
4406285412.9
4506285412.9
4606285412.9
4706285412.9
4806285412.9
4906285412.9
5006285412.9
5106285412.9
5206285412.9
5306285412.9
5406285412.9
5506285412.9
5606285412.9
5706285412.9
5806285412.9
5906285412.9
6006285412.9
6106285412.9
6206285412.9
6306285412.9
6406285412.9
6506285412.9
6606285412.9
6706285412.9
6806285412.9
6906285412.9
7006285412.9
7106285412.9
7206285412.9
7306285412.9
7406285412.9
7506285412.9
Sheet1
 
Upvote 0
Can you confirm and/or clarify:
1.) The raw data starts with the header in BV1 of the source workbook? What is the sheet name (tab name) of the raw data sheet?
2.) You want the binomial output to be in a new workbook or new worksheet ... or ... starting at A10 of the raw data sheet or what?
3). Why do the binomials go to A75 in the example when the total number of WinterAtRisk is only 62? Why don't the zeroes stop at 72?
4.) You want all the binomials for each row to go to consecutive cells in col A? If yes, with an average WinterAtRisk count of 62, the maximum number of raw data rows that can be handled with output to col A would be ~ 16,000. If you have more rows than can be handled in col A, where should the overflow be written?
 
Upvote 0
is that what you want?
(part only because there is 2637 rows)
Alive/LostWinterAtRiskWinterLostWinterAliveWinterLOSS
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
16285412.9%
16285412.9%
16285412.9%
16285412.9%
16285412.9%
16285412.9%
16285412.9%
16285412.9%
03659027524.7%
03659027524.7%
03659027524.7%
03659027524.7%
03659027524.7%
03659027524.7%
03659027524.7%
03659027524.7%
03659027524.7%
03659027524.7%
03659027524.7%
03659027524.7%
 
Upvote 0
is that what you want?
(part only because there is 2637 rows)
Alive/LostWinterAtRiskWinterLostWinterAliveWinterLOSS
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
06285412.9%
16285412.9%
16285412.9%
16285412.9%
16285412.9%
16285412.9%
16285412.9%
16285412.9%
16285412.9%
03659027524.7%
03659027524.7%
03659027524.7%
03659027524.7%
03659027524.7%
03659027524.7%
03659027524.7%
03659027524.7%
03659027524.7%
03659027524.7%
03659027524.7%
03659027524.7%
Yes this is exactly what I need!
 
Upvote 0
Can you confirm and/or clarify:
1.) The raw data starts with the header in BV1 of the source workbook? What is the sheet name (tab name) of the raw data sheet?
2.) You want the binomial output to be in a new workbook or new worksheet ... or ... starting at A10 of the raw data sheet or what?
3). Why do the binomials go to A75 in the example when the total number of WinterAtRisk is only 62? Why don't the zeroes stop at 72?
4.) You want all the binomials for each row to go to consecutive cells in col A? If yes, with an average WinterAtRisk count of 62, the maximum number of raw data rows that can be handled with output to col A would be ~ 16,000. If you have more rows than can be handled in col A, where should the overflow be written?
1. The source sheet has dozens of columns that are useless for my purpose, so I did not include them in the post. The data I can use starts at BV1
2. I would like the output to be in a new sheet ideally.
3. Any issue with the binomials not matching WinterAtRisk is probably an error on my part when putting together the example. The should only be 62.
4. Yes; I haven't dealt with overflow before so don't know exactly how it works, but ideally any overflow would begin in a new sheet
 
Upvote 0
Yes this is exactly what I need!
here is M code for Power Query (Get&Transform)
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Type = Table.TransformColumnTypes(Source,{{"WinterAtRisk", Int64.Type}, {"WinterLost", Int64.Type}, {"WinterAlive", Int64.Type}, {"WinterLOSS", Percentage.Type}}),
    List = Table.AddColumn(Type, "List", each {0..[WinterAtRisk]-1}),
    ELC = Table.ExpandListColumn(List, "List"),
    NID = Table.AddColumn(ELC, "Alive/Lost", each Number.IntegerDivide([List], [WinterAlive]), Int64.Type),
    TSC = Table.SelectColumns(NID,{"Alive/Lost", "WinterAtRisk", "WinterLost", "WinterAlive", "WinterLOSS"})
in
    TSC
 
Upvote 0
1. The source sheet has dozens of columns that are useless for my purpose, so I did not include them in the post. The data I can use starts at BV1
2. I would like the output to be in a new sheet ideally.
3. Any issue with the binomials not matching WinterAtRisk is probably an error on my part when putting together the example. The should only be 62.
4. Yes; I haven't dealt with overflow before so don't know exactly how it works, but ideally any overflow would begin in a new sheet
Here's some code that runs OK on the sample data you provided in post #3. See the comments for assumptions and note I did not have the time to deal with overflow if there is any. No problem with the sample data which require only 2637 rows for the output binomials. Run the code with the sheet holding the raw data active. The output is written to a new sheet the code adds named Output.
VBA Code:
Sub WriteBinomials()
'Assumptions: this macro will be run with the raw data sheet active
'The raw data w/headers will be in BV1:BY? with no other entries below the raw data in col BV
'Output will be written to a new sheet "Output" in cols A:E starting in A2
'Overflow, if any, will need some modification - not included here
Dim R As Range, Vin As Variant, Vout As Variant, dataSht As Worksheet, outputSht As Worksheet
Dim i As Long, m As Long, j As Long, k As Long
Set dataSht = ActiveSheet
Set R = dataSht.Range("BV2:BY" & dataSht.Cells(Rows.Count, "BV").End(xlUp).Row)
Vin = R.Value
With Application
    .ScreenUpdating = False
    .DisplayAlerts = False
End With
On Error Resume Next
Sheets("Output").Delete
On Error GoTo 0
Set outputSht = Sheets.Add
outputSht.Name = "Output"
'calculate # of output areas required for output
    With outputSht.Range("A:E")
        .ClearContents
        .Rows(1).Value = Array("Alive/Lost", "WinterAtRisk", "WinterLost", "WinterAlive", "WinterLOSS")
        .EntireColumn.AutoFit
    End With
    For m = 1 To UBound(Vin, 1)
        ReDim Vout(1 To Vin(m, 1), 1 To UBound(Vin, 2) + 1)
        For j = 1 To UBound(Vout, 1)
            If j <= Vin(m, 2) Then
                Vout(j, 1) = 1
                For k = 2 To 5
                    Vout(j, k) = Vin(m, k - 1)
                Next k
            Else
                Vout(j, 1) = 0
                For k = 2 To 5
                    Vout(j, k) = Vin(m, k - 1)
                Next k
            End If
        Next j
    outputSht.Range("A" & Cells(Rows.Count, "A").End(xlUp).Row).Offset(1).Resize(UBound(Vout, 1), 5).Value = Vout
    Erase Vout
    Next m
With Application
    .ScreenUpdating = True
    .DisplayAlerts = True
End With
End Sub
 
Upvote 0
Here's a modified version that handles overflow - in this case overflow is defined as output that requires more than one-million rows.
VBA Code:
Sub WriteBinomials_1()
'Assumptions: this macro will be run with the raw data sheet active
'The raw data w/headers will be in BV1:BY? with no other entries below the raw data in col BV
'Output will be written to a new sheet "Output" in cols A:E starting in A2
'output exceeding BlockCount rows will be redirected to cols G:K, M:Q, ...
Const BlockCount As Long = 1000000  'Number of rows in a block of output - not to exceed rows.count-1
Dim Rall As Range, Vall As Variant
Dim R() As Range, Rin As Range, Vin As Variant, Vout As Variant, dataSht As Worksheet, outputSht As Worksheet
Dim i As Long, m As Long, j As Long, k As Long, Ars As Long, First As Long, Last As Long, Ct As Long
Set dataSht = ActiveSheet
Set Rall = dataSht.Range("BV2:BY" & dataSht.Cells(Rows.Count, "BV").End(xlUp).Row)
Vall = Rall.Value
If Val(Vall(1, 1)) > BlockCount Then
    MsgBox "WinterAtRisk values must be <= " & BlockCount
    Exit Sub
End If
Ars = 1 + Round(Application.Sum(Rall.Columns(1)) / BlockCount, 0) 'number of areas needed for output with overflow
For i = 1 To UBound(Vall, 1)
    S = S + Val(Vall(i, 1))
    If S > BlockCount Then
        First = Last + 1
        Last = i - 1
        Ct = Ct + 1
        ReDim Preserve R(1 To Ct)
        Set R(Ct) = dataSht.Range(Rall.Rows(First), Rall.Rows(Last))
        S = Val(Vall(i, 1))
    ElseIf i = UBound(Vall, 1) Then
        First = Last + 1
        Last = i
        Ct = Ct + 1
        ReDim Preserve R(1 To Ct)
        Set R(Ct) = dataSht.Range(Rall.Rows(First), Rall.Rows(Last))
    End If
Next i
With Application
    .ScreenUpdating = False
    .DisplayAlerts = False
End With
On Error Resume Next
Sheets("Output").Delete
On Error GoTo 0
Set outputSht = Sheets.Add
outputSht.Name = "Output"
    For i = 1 To Ars
        With outputSht.Range("A:E").Offset(0, (i - 1) * 6).Resize(1, 5)
            .ClearContents
            .Rows(1).Value = Array("Alive/Lost", "WinterAtRisk", "WinterLost", "WinterAlive", "WinterLOSS")
            .EntireColumn.AutoFit
        End With
    Next i
For i = 1 To Ars
    Set Rin = R(i)
    Vin = Rin.Value
    For m = 1 To UBound(Vin, 1)
        ReDim Vout(1 To Vin(m, 1), 1 To UBound(Vin, 2) + 1)
        For j = 1 To UBound(Vout, 1)
            If j <= Vin(m, 2) Then
                Vout(j, 1) = 1
                For k = 2 To 5
                    Vout(j, k) = Vin(m, k - 1)
                Next k
            Else
                Vout(j, 1) = 0
                For k = 2 To 5
                    Vout(j, k) = Vin(m, k - 1)
                Next k
            End If
        Next j
    outputSht.Cells(Rows.Count, 1 + (i - 1) * 6).End(xlUp).Offset(1).Resize(UBound(Vout, 1), 5).Value = Vout
    Erase Vout
    Next m
Next i
With Application
    .ScreenUpdating = True
    .DisplayAlerts = True
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,651
Messages
6,120,744
Members
448,989
Latest member
mariah3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top