Dear R-team, I have written this code for calculation my data file. But there shows 'subscript out of bounds?.> computeResponse <- function(data){+ dataodd <- data[-(1:18),] + dataodd <- dataodd[seq(1,nrow(dataodd), 2),] + hitsodd <- table(factor(dataodd[,5]), factor(dataodd[,15]))[2,2] + missesodd <- table(factor(dataodd[,5]), factor(dataodd[,15]))[2,1] + crejectionsodd <- table(factor(dataodd[,5]), factor(dataodd[,15]))[1,2] + falsealarmodd <- table(factor(dataodd[,5]), factor(dataodd[,15]))[1,1] + return(coordinationodd <- data.frame(hitsodd, missesodd,crejectionsodd, falsealarmodd)) + }> > filenames <- list.files(full.names=TRUE) > filelist <- lapply(filenames, read.table, fill = TRUE, header = FALSE, sep = "\t") > coordinationodd <- lapply(filelist, computeResponse)Error in table(factor(dataodd[, 5]), factor(dataodd[, 15]))[2, 2] : subscript out of bounds> coordinationodd <- Reduce(rbind, coordinationodd)Error in Reduce(rbind, coordinationodd) : object 'coordinationodd' not found Could you please tell me how I can fix this problem? Best, Samsad [[alternative HTML version deleted]]
You don't provide a reproducible example - what does your data look like? If this were my problem, I would start by working through the function step by step with one of my files data <- filelist[[1]] dataodd <- data[-(1:18),] and so on, examining the result at every step with tools like dim() and str() until I found the problem. There are more fully-developed debugging tools available, but for such a short function I wouldn't bother. Sarah On Tue, Jul 12, 2016 at 9:29 AM, Samsad Afrin Himi <samsad.afrin at gmail.com> wrote:> Dear R-team, > > I have written this code for calculation my data file. But there shows 'subscript out of bounds?. > > > >> computeResponse <- function(data){ > + dataodd <- data[-(1:18),] > + dataodd <- dataodd[seq(1,nrow(dataodd), 2),] > + hitsodd <- table(factor(dataodd[,5]), factor(dataodd[,15]))[2,2] > + missesodd <- table(factor(dataodd[,5]), factor(dataodd[,15]))[2,1] > + crejectionsodd <- table(factor(dataodd[,5]), factor(dataodd[,15]))[1,2] > + falsealarmodd <- table(factor(dataodd[,5]), factor(dataodd[,15]))[1,1] > + return(coordinationodd <- data.frame(hitsodd, missesodd,crejectionsodd, falsealarmodd)) > + } >> >> filenames <- list.files(full.names=TRUE) >> filelist <- lapply(filenames, read.table, fill = TRUE, header = FALSE, sep = "\t") >> coordinationodd <- lapply(filelist, computeResponse) > Error in table(factor(dataodd[, 5]), factor(dataodd[, 15]))[2, 2] : > subscript out of bounds >> coordinationodd <- Reduce(rbind, coordinationodd) > Error in Reduce(rbind, coordinationodd) : > object 'coordinationodd' not found > > > Could you please tell me how I can fix this problem? > > Best, > Samsad > [[alternative HTML version deleted]] >
Include the levels argument in your calls to factor so the tables all have the same dimensions.> table(factor((1:4)>2), factor( (1:4)>0 ))TRUE FALSE 2 TRUE 2> table(factor((1:4)>2, levels=c(FALSE,TRUE)), factor( (1:4)>0,levels=c(FALSE,TRUE) )) FALSE TRUE FALSE 0 2 TRUE 0 2 Trying to extract row 2, column 2 from the former will give a subscript-out- of-bounds error. Bill Dunlap TIBCO Software wdunlap tibco.com On Tue, Jul 12, 2016 at 6:29 AM, Samsad Afrin Himi <samsad.afrin at gmail.com> wrote:> Dear R-team, > > I have written this code for calculation my data file. But there shows > 'subscript out of bounds?. > > > > > computeResponse <- function(data){ > + dataodd <- data[-(1:18),] > + dataodd <- dataodd[seq(1,nrow(dataodd), 2),] > + hitsodd <- table(factor(dataodd[,5]), factor(dataodd[,15]))[2,2] > + missesodd <- table(factor(dataodd[,5]), factor(dataodd[,15]))[2,1] > + crejectionsodd <- table(factor(dataodd[,5]), > factor(dataodd[,15]))[1,2] > + falsealarmodd <- table(factor(dataodd[,5]), > factor(dataodd[,15]))[1,1] > + return(coordinationodd <- data.frame(hitsodd, > missesodd,crejectionsodd, falsealarmodd)) > + } > > > > filenames <- list.files(full.names=TRUE) > > filelist <- lapply(filenames, read.table, fill = TRUE, header = FALSE, > sep = "\t") > > coordinationodd <- lapply(filelist, computeResponse) > Error in table(factor(dataodd[, 5]), factor(dataodd[, 15]))[2, 2] : > subscript out of bounds > > coordinationodd <- Reduce(rbind, coordinationodd) > Error in Reduce(rbind, coordinationodd) : > object 'coordinationodd' not found > > > Could you please tell me how I can fix this problem? > > Best, > Samsad > [[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.[[alternative HTML version deleted]]