How to break one column to multiple rows with each cell row limit
up vote
0
down vote
favorite
Thank you for your time.
I am unable to figure out a way to perform the following operation.
The logic for the operation is to break down a column to multiple rows with limits on each cell value.
for 1st row and second row max is 250. subsequent are 500 max. each value in amount should be broken down to multiple rows based on these set of rules.
for ex:- ID 1 has 1500 as amount. when being broken down. the first row and second row can allow max 250 as a values. and next rows accommodate the rest until the amount value is maxed out.
Data is shown below
Raw data
ID Name Amount
1 aa 1500
2 bb 2000
3 cc 1000
4 dd 500
Final output
ID Name Amount
1 aa 250
1 aa 250
1 aa 500
1 aa 500
2 bb 250
2 bb 250
2 bb 500
2 bb 500
2 bb 500
2 bb 500
3 cc 250
3 cc 250
3 cc 500
4 dd 250
4 dd 250
r
add a comment |
up vote
0
down vote
favorite
Thank you for your time.
I am unable to figure out a way to perform the following operation.
The logic for the operation is to break down a column to multiple rows with limits on each cell value.
for 1st row and second row max is 250. subsequent are 500 max. each value in amount should be broken down to multiple rows based on these set of rules.
for ex:- ID 1 has 1500 as amount. when being broken down. the first row and second row can allow max 250 as a values. and next rows accommodate the rest until the amount value is maxed out.
Data is shown below
Raw data
ID Name Amount
1 aa 1500
2 bb 2000
3 cc 1000
4 dd 500
Final output
ID Name Amount
1 aa 250
1 aa 250
1 aa 500
1 aa 500
2 bb 250
2 bb 250
2 bb 500
2 bb 500
2 bb 500
2 bb 500
3 cc 250
3 cc 250
3 cc 500
4 dd 250
4 dd 250
r
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Thank you for your time.
I am unable to figure out a way to perform the following operation.
The logic for the operation is to break down a column to multiple rows with limits on each cell value.
for 1st row and second row max is 250. subsequent are 500 max. each value in amount should be broken down to multiple rows based on these set of rules.
for ex:- ID 1 has 1500 as amount. when being broken down. the first row and second row can allow max 250 as a values. and next rows accommodate the rest until the amount value is maxed out.
Data is shown below
Raw data
ID Name Amount
1 aa 1500
2 bb 2000
3 cc 1000
4 dd 500
Final output
ID Name Amount
1 aa 250
1 aa 250
1 aa 500
1 aa 500
2 bb 250
2 bb 250
2 bb 500
2 bb 500
2 bb 500
2 bb 500
3 cc 250
3 cc 250
3 cc 500
4 dd 250
4 dd 250
r
Thank you for your time.
I am unable to figure out a way to perform the following operation.
The logic for the operation is to break down a column to multiple rows with limits on each cell value.
for 1st row and second row max is 250. subsequent are 500 max. each value in amount should be broken down to multiple rows based on these set of rules.
for ex:- ID 1 has 1500 as amount. when being broken down. the first row and second row can allow max 250 as a values. and next rows accommodate the rest until the amount value is maxed out.
Data is shown below
Raw data
ID Name Amount
1 aa 1500
2 bb 2000
3 cc 1000
4 dd 500
Final output
ID Name Amount
1 aa 250
1 aa 250
1 aa 500
1 aa 500
2 bb 250
2 bb 250
2 bb 500
2 bb 500
2 bb 500
2 bb 500
3 cc 250
3 cc 250
3 cc 500
4 dd 250
4 dd 250
r
r
edited Nov 9 at 20:56
hrbrmstr
58.3k584143
58.3k584143
asked Nov 9 at 20:46
ShasankC
11
11
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
Here's how you could do it with loops. Won't be too bad if your data isn't too large.
x = data.frame("ID"=1:5, "Name"=c("aa","bb","cc","dd","ee"),"Amount"=c(1500,2000,1000,500,800))
do_that = function(x)
y = x[0,]
r = 0
for (i in 1:NROW(x))
count = 0
while (x[i,3] > 0)
r = r + 1
y[r,1] = x[i,1]
y[r,2] = x[i,2]
y[r,3] = 0
count = count + 1
if (count <= 2)
d = min(x[i,3], 250)
else
d = min(x[i,3], 500)
y[r,3] = y[r,3] + d
x[i,3] = x[i,3] - d
return (y)
y = do_that(x)
y
I even added a bonus row that's not a multiple of 250 or 500.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Here's how you could do it with loops. Won't be too bad if your data isn't too large.
x = data.frame("ID"=1:5, "Name"=c("aa","bb","cc","dd","ee"),"Amount"=c(1500,2000,1000,500,800))
do_that = function(x)
y = x[0,]
r = 0
for (i in 1:NROW(x))
count = 0
while (x[i,3] > 0)
r = r + 1
y[r,1] = x[i,1]
y[r,2] = x[i,2]
y[r,3] = 0
count = count + 1
if (count <= 2)
d = min(x[i,3], 250)
else
d = min(x[i,3], 500)
y[r,3] = y[r,3] + d
x[i,3] = x[i,3] - d
return (y)
y = do_that(x)
y
I even added a bonus row that's not a multiple of 250 or 500.
add a comment |
up vote
0
down vote
Here's how you could do it with loops. Won't be too bad if your data isn't too large.
x = data.frame("ID"=1:5, "Name"=c("aa","bb","cc","dd","ee"),"Amount"=c(1500,2000,1000,500,800))
do_that = function(x)
y = x[0,]
r = 0
for (i in 1:NROW(x))
count = 0
while (x[i,3] > 0)
r = r + 1
y[r,1] = x[i,1]
y[r,2] = x[i,2]
y[r,3] = 0
count = count + 1
if (count <= 2)
d = min(x[i,3], 250)
else
d = min(x[i,3], 500)
y[r,3] = y[r,3] + d
x[i,3] = x[i,3] - d
return (y)
y = do_that(x)
y
I even added a bonus row that's not a multiple of 250 or 500.
add a comment |
up vote
0
down vote
up vote
0
down vote
Here's how you could do it with loops. Won't be too bad if your data isn't too large.
x = data.frame("ID"=1:5, "Name"=c("aa","bb","cc","dd","ee"),"Amount"=c(1500,2000,1000,500,800))
do_that = function(x)
y = x[0,]
r = 0
for (i in 1:NROW(x))
count = 0
while (x[i,3] > 0)
r = r + 1
y[r,1] = x[i,1]
y[r,2] = x[i,2]
y[r,3] = 0
count = count + 1
if (count <= 2)
d = min(x[i,3], 250)
else
d = min(x[i,3], 500)
y[r,3] = y[r,3] + d
x[i,3] = x[i,3] - d
return (y)
y = do_that(x)
y
I even added a bonus row that's not a multiple of 250 or 500.
Here's how you could do it with loops. Won't be too bad if your data isn't too large.
x = data.frame("ID"=1:5, "Name"=c("aa","bb","cc","dd","ee"),"Amount"=c(1500,2000,1000,500,800))
do_that = function(x)
y = x[0,]
r = 0
for (i in 1:NROW(x))
count = 0
while (x[i,3] > 0)
r = r + 1
y[r,1] = x[i,1]
y[r,2] = x[i,2]
y[r,3] = 0
count = count + 1
if (count <= 2)
d = min(x[i,3], 250)
else
d = min(x[i,3], 500)
y[r,3] = y[r,3] + d
x[i,3] = x[i,3] - d
return (y)
y = do_that(x)
y
I even added a bonus row that's not a multiple of 250 or 500.
answered Nov 9 at 21:09
mickey
58114
58114
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53233037%2fhow-to-break-one-column-to-multiple-rows-with-each-cell-row-limit%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown