Hey all, I would like to know if anyone, can put in the right direction of the following problem: I am currently want to use it to check if a column with a length of 61327 is consistent over an 1 to 157 interval until the end of the column. In the case the interval is interrupted I want to know which values are missing and where the missing values are located. I started of with the following code to assign 1s, if we have a number ? 157 and 0 if not. I tried to do a double loop: n=61327 Control = matrix( 0, nrow = n, ncol = 1) for (i in length(FD$WEEK)) { for (j in 1:157) { if(FD$WEEK[j] <= 157) { Control[,1] = 1 } else { Control[,1] = 0 } } } I believe that this code is not correct, but I am unable to wrap my head around how I can check that the interval always will be followed. All the best, Christoph [[alternative HTML version deleted]]
Hello, I'm not at all sure if the following is what you need but instead of for (i in length(FD$WEEK)) try for (i in 1:length(FD$WEEK)) or even better for (i in seq_len(FD$WEEK)) And use Control[i, 1], not Control[, 1] Hope thi helps, Rui Barradas Citando Christoph Puschmann <c.puschmann at student.unsw.edu.au>:> Hey all, > > I would like to know if anyone, can put in the right direction of > the following problem: > > I am currently want to use it to check if a column with a length of > 61327 is consistent over an 1 to 157 interval until the end of the > column. In the case the interval is interrupted I want to know which > values are missing and where the missing values are located. I > started of with the following code to assign 1s, if we have a number > ? 157 and 0 if not. > > > > I tried to do a double loop: > > > > n=61327 > Control = matrix( > 0, > nrow = n, > ncol = 1) > > > > for (i in length(FD$WEEK)) { > for (j in 1:157) { > if(FD$WEEK[j] <= 157) { > Control[,1] = 1 > } else { > Control[,1] = 0 > } > } > } > > > > I believe that this code is not correct, but I am unable to wrap my > head around how I can check that the interval always will be followed. > > All the best, > > Christoph > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
It would help to have a minimal, reproducible example. Unless revealing the structure of your FD object, it is difficult to understand how a column having 61327 values would be "consistent over an 1 to 157 interval": is this interval cyclic until it reaches 61327 values?>From your example using FD$WEEK, you are using a column called WEEK withina dataframe names FD, and you only loop over the first 157 values of that column. So where is the "column having 61327 values"? For these types of problems you don't even need a loop, R is a vectorised language (please note that you have a double loop but never use the "i" one). Very unclear, so please try to create a MRE, as the posting guide advices. On Fri, Oct 7, 2016 at 11:22 PM, Christoph Puschmann < c.puschmann at student.unsw.edu.au> wrote:> Hey all, > > I would like to know if anyone, can put in the right direction of the > following problem: > > I am currently want to use it to check if a column with a length of 61327 > is consistent over an 1 to 157 interval until the end of the column. In the > case the interval is interrupted I want to know which values are missing > and where the missing values are located. I started of with the following > code to assign 1s, if we have a number ? 157 and 0 if not. > > > > I tried to do a double loop: > > > > n=61327 > Control = matrix( > 0, > nrow = n, > ncol = 1) > > > > for (i in length(FD$WEEK)) { > for (j in 1:157) { > if(FD$WEEK[j] <= 157) { > Control[,1] = 1 > } else { > Control[,1] = 0 > } > } > } > > > > I believe that this code is not correct, but I am unable to wrap my head > around how I can check that the interval always will be followed. > > All the best, > > Christoph > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/ > posting-guide.html > and provide commented, minimal, self-contained, reproducible code.-- Adrian Dusa University of Bucharest Romanian Social Data Archive Soseaua Panduri nr.90 050663 Bucharest sector 5 Romania [[alternative HTML version deleted]]
Dear Adrian, Yes it is a cyclical data set and theoretically it should repeat this interval until 61327. The data set itself is divided into 2 Parts: 1. Product category (column 10) 2. Number of Stores Participating (column 01) Overall there are 22 different products and in each you have 19 different stores participating. And theoretically each store over each product category should have a 1 - 157 week interval. The part I am struggling with is how do I run a loop over the whole data set, while checking if all stores participated 157 weeks over the different products. So far I came up with this: n=61327 # Generate Matrix to check for values Control = matrix( 0, nrow = n, ncol = 1) s <- seq(from =1 , to = 157, by = 1) CW = matrix( s, nrow = 157, ncol = 1 ) colnames(CW)[1] <- ?s' CW = as.data.frame(CW) for (i in 1:nrow(FD)) { # Let run trhough all the rows for (j in 1:157) { if(FD$WEEk[j] == C$s[j]) { Control[i] = 1 # coresponding control row = 1 } else { Control[i] = 0 # corresponding control row = 0 } } } I coded a MRE and attached an sample of my data set. MRE: #MRE dat <- data.frame( Store = c(rep(8, times = 157), rep(12, times = 157)), # Number of stores WEEK = rep(seq(from=1, to = 157, by = 1), times = 2) )