Minimum remainder in range of multiple of integer for an integer - math

Is it possible to resolve below given problem with excel formula?
One machine can produce in lot ( range of integer between two numbers)
For example, it can produce in multiple of 5 to 7 batches per lot ( can produce in multiple of 5,6 & 7 ). I have one order of 8 batches , now I want to compute that how many MINIMUM extra batches needs to be produce to fulfill the lot size requirement.
Answer for this question is = 2 ( I can produce 5+5=10 ( 8 order + 2 extra ).
Variable value
Variable value
Variable value
Want formula to calculate this
For ref
Min capability
Max capability
Order
Extra batch to fulfill order
Lot combi
5
7
1
4
5
5
7
2
3
5
5
7
3
2
5
5
7
4
1
5
5
7
5
0
5
5
7
6
0
6
5
7
7
0
7
5
7
8
2
5+5
5
7
9
1
5+5
5
7
10
0
5+5
5
7
11
0
5+6
5
7
12
0
5+7
5
7
13
0
6+7
5
7
14
0
7+7
5
7
15
0
5+5+5
5
7
16
0
5+5+6
...
...
...
...
...

Related

Creating an repeating but increasing sequence in R [duplicate]

This question already has answers here:
Using seq and rep to create a sequence of 5 integers that go up by 1 on each repetition
(4 answers)
Closed 1 year ago.
I want to create the sequence 1 2 3 4 5 2 3 4 5 6 3 4 5 6 7 4 5 6 7 8 5 6 7 8 9 if possible using only rep and 'seq'. So each repetition I want the repeating sequence to increase by one. This could be achieved my creating rep(seq(1,5),5) and then adding a vector rep(0:4, each = 5).
But is there any way to do this without creating a new vector and adding it to the first one?
You can use outer + seq in one line
> c(outer(seq(5), seq(5) - 1, `+`))
[1] 1 2 3 4 5 2 3 4 5 6 3 4 5 6 7 4 5 6 7 8 5 6 7 8 9
or shorter code with embed
> c(embed(1:9, 5)[, 5:1])
[1] 1 2 3 4 5 2 3 4 5 6 3 4 5 6 7 4 5 6 7 8 5 6 7 8 9

Trouble splitting up data evenly?

I am trying to split up data evenly in R. For example, I am using the dataset cars that is built into R Studio with 50 lines. If I want to split the data into two sections, I would do something along the lines of this:
cars$split <- rep(1:2, each=25) where I would create a column called split and assign the first 25 values to a 1, and the next 25 values to a 2. However, if I wanted to split my data into, lets say, 8 sections (based on user discretion), I would not be able to divide 50 / 8 evenly as it equals to 6.25. In this case, I would simply assign the last two rows (since 50 / 8 = 6.25, and 6 * 8 = 48 so we would have 2 rows left over) to the number 8 in this case using the function above. However, I am unable to do this since the rep function needs to divide properly so I tried to write out some logic as so, but I get an issue saying:
Error in `$<-.data.frame`(`*tmp*`, "split", value = c(1L, 1L, 1L, 1L, : replacement has 48 rows, data has 50
Any ideas on how to fix this? My attempt is shown below:
numDataPerSection <- floor(nrow(cars) / userInputNum)
if(nrow(cars) %% userInputNum != 0){
#If not divisible, assign last few data points to the last number
cars$split <- rep(1:ncls, each=numDataPerSection, len = nrow(cars) - (nrow(cars) %% userInputNum))
for(i in nrow(cars) %% userInputNum){
cars$split[nrow(cars) - i] <- userInputNum
}
}
#Everything divides correctly
else{
cars$split <- rep(1:ncls, each=numDataPerSection)
}
How about using a function such as this one to create your indices?
create.indices <- function(nrows, sections) {
indices <- rep(1:sections,each=floor(nrows/sections))
indices <- append(indices, rep(tail(indices, 1), nrows%%sections))
return(indices)
}
create.indices(50,8)
# [1] 1 1 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3 3 4 4 4 4 4 4 5 5 5 5 5 5 6 6 6 6 6 6 7 7 7 7 7 7 8 8 8 8 8 8 8 8
You could use something like
split1 <- function(n,s){ c( rep(1:s, each=n%/%s), rep(s, n%%s) ) }
cars$split <- split1(nrow(cars,userInputNum))
but this is not very balanced as in your example category 8 is two larger than any other, and would be worse with 55 rows and 8 sections:
> split1(50,8)
[1] 1 1 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3 3 4 4 4 4 4 4 5 5 5 5 5 5 6 6 6 6 6 6 7 7
[39] 7 7 7 7 8 8 8 8 8 8 8 8
> table(split1(50,8))
1 2 3 4 5 6 7 8
6 6 6 6 6 6 6 8
> table(split1(55,8))
1 2 3 4 5 6 7 8
6 6 6 6 6 6 6 13
You could do better with something like
split2 <- function(n,s){ ((1:n)*s+n-s) %/% n }
which produces
> split2(50,8)
[1] 1 1 1 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3 3 4 4 4 4 4 4 5 5 5 5 5 5 5 6 6 6 6 6 6
[39] 7 7 7 7 7 7 8 8 8 8 8 8
> table(split2(50,8))
1 2 3 4 5 6 7 8
7 6 6 6 7 6 6 6
> table(split2(55,8))
1 2 3 4 5 6 7 8
7 7 7 7 7 7 7 6
You can use the length.out argument of rep() to create your split column: rep(1:8, length.out = 50, each = round(50/8)). Using the round() function works reasonably well to achieve a uniform distribution of group sizes:
> table(rep(1:8, length.out = 50, each = round(50/8)))
1 2 3 4 5 6 7 8
8 6 6 6 6 6 6 6

Delete single occurances in longitudinal data

I am working with longitudinal data. I want to remove the observations of people that were only measured once (ids 5,7,9 below). How do I do this? Assume id is the unique identifier for people in the data set. Therefore, I would want to remove observations associated with ids 5,7, and 9. I've played with duplicated, unique, the table function, and the count function in plyr but haven't been successful. Example data below.
y<-sample(1:10, 20, replace=TRUE)
x<-sample(c(0,1),20, replace=TRUE)
id<-c(1,1,1,2,2,2,3,3,3,4,4,4,5,6,6,7,8,8,8,9)
data<-data.frame(cbind(y,x,id))
You would have received immediate assistance had you tagged the post as R,data.frame
Here, the ! "not" function is used to remove id rows which match the values c(5,7,9)
> data[!data$id %in% c(5,7,9),]
y x id
1 3 0 1
2 2 1 1
3 3 0 1
4 9 0 2
5 9 0 2
6 1 0 2
7 9 0 3
8 7 0 3
9 4 0 3
10 9 1 4
11 7 0 4
12 8 1 4
14 4 1 6
15 1 0 6
17 2 0 8
18 8 0 8
19 2 0 8

R indirect reference in data frame

I would like to refer to values in a data frame column with the row index being dependent on the value of another column.
Example:
value lag laggedValue
1 1 2
2 2 4
3 3 6
4 2 6
5 1 6
6 3 9
7 3 10
8 1 9
9 1 10
10 2
In Excel I use this formula in column "laggedValue":
=INDIRECT("B"&(ROW(B2)+C2))
How can I do this in an R data frame?
Thanks!
For row r with associated lag value lag[r] it looks like you're trying to create a new column that is the (r+lag[r])th element of value (or a missing value if this is out of bounds). You can do this with:
dat$laggedValue <- dat$value[seq(nrow(dat)) + dat$lag]
dat
value lag laggedValue
1 1 1 2
2 2 2 4
3 3 3 6
4 4 2 6
5 5 1 6
6 6 3 9
7 7 3 10
8 8 1 9
9 9 1 10
10 10 2 NA
Other commenters are mentioning that it looks like you're just adding the value and lag columns because your value column has the elements 1 through 10, but this solution will work even when your value column has other data stored in it.
Assuming the same thing as #rawr here:
dat <- data.frame(value=c(1:10),
lag=c(1,2,3,2,1,3,3,1,1,2))
dat$laggedValue <- dat$value + dat$lag
dat
value lag laggedValue
1 1 1 2
2 2 2 4
3 3 3 6
4 4 2 6
5 5 1 6
6 6 3 9
7 7 3 10
8 8 1 9
9 9 1 10
10 10 2 12

Multiply the rows of a matrix to get a vector: J, j701

I am programming with J.
I have this vector:
F =: 5>\i.10
F
0 1 2 3 4
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
5 6 7 8 9
How can I have this vector as result:
(*/ 0 1 2 3 4), (*/ 1 2 3 4 5), (*/ 2 3 4 5 6), (*/ 3 4 5 6 7), (*/ 4 5 6 7 8), (*/ 5 6 7 8 9)
0 120 720 2520 6720 15120
NB. I want to multiply all the rows
I tried:
*/ F
0 720 5040 20160 60480
but, how you can see it multiply the columns, and I want the rows.
How can I use the */ to multiply the rows? Thank you all!
In short, what you want is 5 */\ i.10
5 */\ i.10
0 120 720 2520 6720 15120
However, if you ever run across this issue in another context, and you really want to address the rows, you could say:
]M=:5>\i. 10
0 1 2 3 4
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
5 6 7 8 9
*/ rows M
0 120 720 2520 6720 15120
Rows is defined by the standard library as "1. That is, it applies the verb at "rank 1". Rank is a fundamental concept in J, and you'll need to understand it to progress with the language.

Resources