Error: subscript out of bounds - what does this mean?

34,078 views
Skip to first unread message

Ias S

unread,
Oct 21, 2016, 8:47:32 AM10/21/16
to unmarked
HI,

I am using data that has been put into a unmarkedFrameGDS.
However, when I try a simple null model is get the following error?

Any help would be appreciated.

Thanks

Ian

db <- c(0, 10, 25, 50, 100, 200)

#numPrimary=nVisits

#load separate table with covariates at observer level ##in correst
obsCovs1 <- read.csv("covs_obs_level_ing.csv", header=T)

y <- as.matrix(yDat)
wt<- data.frame(covs)
str(wt)
siteCovs <- wt[,c("standage", "cov.tree")]
bird.obsCovs<- list(date=obsCovs1[,c("Day.1", "Day.2", "Day.3", "Day.4", "Day.5")], time=obsCovs1[,c("Time.1",  "Time.2", "Time.3", "Time.4", "Time.5")])


umf <- unmarkedFrameGDS(y=y, siteCovs=siteCovs, yearlySiteCovs = bird.obsCovs, survey="point",  dist.breaks=db, numPrimary=5,   unitsIn="m")
head(umf)
summary(umf)  

# Fit the model
m1 <- gdistsamp(~1, ~1, ~1, umf, output="density")
summary(m1)

Ias S

unread,
Oct 21, 2016, 8:48:15 AM10/21/16
to unmarked
This was the error:
> m1 <- gdistsamp(~1, ~1, ~1, umf, output="density")
Error in `[<-`(`*tmp*`, i, j, value = 1649.33614313464) : 
  subscript out of bounds

Florent Bled

unread,
Oct 21, 2016, 10:51:39 PM10/21/16
to unmarked
Hello Ias,

This issue usually appears when you try to access a column or a row in a dataframe that does not exist. You should double-check the structure of your data. Without a look at your actual data, it will be difficult to pin point exactly where the problem is coming from. If you want to, you can post here a subset of the data that reproduces the error. If you don't want to publicly do that, you can send me directly some data to take a look at it.

Cheers,

Florent

priyanka ratnakar

unread,
May 7, 2018, 6:44:54 AM5/7/18
to unmarked
Hi Florent,

I a getting the same error.Can you please suggest a solution 

for(i in 1:dim(dataset)[1]){
  hitlist=trainmy2[trainmy2$VisitNumber==dataset[i,2],]
  dayofweek=names(hitlist)[3:9][hitlist[1,3:9]>0]
  dataset[i,dayofweek]=1
  hitlist2=Copy_Walmart[Copy_Walmart$VisitNumber==dataset[i,2],]
  agg=aggregate(ScanCount~DepartmentDescription,data=hitlist2,sum)
  sub1<-gsub("", ".",paste0(names(agg)[1],".",agg[,1]))
  sub2<-gsub("-", ".",sub1)
  sub3<-gsub("&", ".",sub2)
  sub4<-gsub("/", ".",sub3)
  sub5<-gsub(",", ".",sub4)
  dataset[i,sub5]=agg[,2]
  print(i)
}

Error:- Error in `*tmp*`[[j]] : subscript out of bounds

Florent Bled

unread,
May 7, 2018, 10:54:26 PM5/7/18
to unmarked
Hello priyanka!

Without an actual piece of the dataset from which to reproduce the error, it is difficult to figure out exactly what's happening. If you can't share a reproducible example, the way I would go about identifying where the issue is coming from is to first check where in the loop you're stopping (as easy as simply typing "i" and enter, that will print the latest value of i).   Then, I'd run each line in the loop individually, one after the other, using this value of i. This way, you'd be able to identify which instruction is causing the error. From there, you'll be able to look at each element, and make sure that everything is indexed properly / has the correct format.
I hope it helps!

Florent

priyanka ratnakar

unread,
May 8, 2018, 12:17:12 AM5/8/18
to unma...@googlegroups.com
Thanks Florent it did helped. 

--
You received this message because you are subscribed to the Google Groups "unmarked" group.
To unsubscribe from this group and stop receiving emails from it, send an email to unmarked+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ian F

unread,
Jul 12, 2018, 7:43:25 PM7/12/18
to unmarked

I am having a similar issue and have no idea whats wrong. any suggestions? I am trying to alter this script to fit my data set which has 4 rows and 10 columns. i keep getting the error code Error in `[<-`(`*tmp*`, j, 1, value = sub.data[1, 1]) : 
  subscript out of bounds. can anyone help?


data <- read.table(chr, header=TRUE) 

len <- length(data$X)
  
overall.table <- matrix(nrow=len/4, ncol=(5*2)+0)
  
overall.length <- len/4 
  
for(j in c(1:overall.length)) {
  
print(j) #displays entire object j

sub.data <- as.matrix(data[ (1+4*(j-1)):((1+4*(j-1))+11), 2:4])

overall.table[j,1] <- sub.data[1, 1]
overall.table[j,2:3] <- sub.data[1, 2:3]
overall.table[j,4:5] <- sub.data[2, 2:3]
overall.table[j,6:7] <- sub.data[3, 2:3]
overall.table[j,8:9] <- sub.data[4, 2:3]
overall.table[j,10:11] <- sub.data[5, 2:3]
overall.table[j,12:13] <- sub.data[6, 2:3]
overall.table[j,14:15] <- sub.data[7, 2:3]
overall.table[j,16:17] <- sub.data[8, 2:3]
overall.table[j,18:19] <- sub.data[9, 2:3]
overall.table[j,20:21] <- sub.data[10, 2:3]
overall.table[j,22:23] <- sub.data[11, 2:3]
overall.table[j,24:25] <- sub.data[12, 2:3]
overall.table[j,26:27] <- sub.data[13, 2:3]
overall.table[j,28:29] <- sub.data[14, 2:3]
overall.table[j,30:31] <- sub.data[15, 2:3]
overall.table[j,32:33] <- sub.data[16, 2:3]
overall.table[j,34:35] <- sub.data[17, 2:3]

Ian F

unread,
Jul 12, 2018, 7:43:25 PM7/12/18
to unmarked
This is the data set im working with.
order label x y
1 rafalt.JPG 764 884
2 rafalt.JPG 2080 724
3 rafalt.JPG 3480 936
4 rafalt.JPG 2036 2720
1 rafnas.JPG 1552 1140
2 rafnas.JPG 2156 1020
3 rafnas.JPG 2788 1104
4 rafnas.JPG 2172 2048
1 Rafsp.JPG 1324 1008
2 Rafsp.JPG 2092 840
3 Rafsp.JPG 2964 924
4 Rafsp.JPG 2132 2356
To unsubscribe from this group and stop receiving emails from it, send an email to unmarked+u...@googlegroups.com.

Jim Baldwin

unread,
Jul 13, 2018, 2:08:40 AM7/13/18
to unma...@googlegroups.com
IanF,

Your problem seems to be set of R problems rather than anything to do with unmarked.  Case matters:  You use  len <- length(data$X) when the data supplied uses a lowercase x.

But the main problem is that you're attempting to reference rows and columns that don't exist.  sub.data is a 12 x 3 matrix and overall.table is a 3 x 10 matrix.  Therefore, you can't request  overall.table[j,12:13] or sub.data[13, 2:3].

Jim

Ian F

unread,
Jul 14, 2018, 1:05:53 PM7/14/18
to unmarked
Thank you very much for your help Jim it finally worked.

Linh Phạm

unread,
Aug 14, 2019, 11:46:38 AM8/14/19
to unmarked
Hello,
I am also having problem with this error

> library(stringr)
> text <- list()
> htemp <- list()
> htags <- data.frame()
> data <- read.csv("0908 clean csv.csv", sep = ";")
> data <- as.matrix(data[-1])
> maxrows <- nrow(data)
> for(i in 1:maxrows){
+     text[i] <- as.character(data[i,5])
+     htemp <- str_extract_all(text[i], "#\\S+", TRUE)
+     
+     if(ncol(htemp) != 0){
+         for(j in 1:ncol(htemp)){
+             htags[i,j] <- htemp[1,j]
+         }  
+     }
+ } 
Error in data[i, 5] : subscript out of bounds
> #Save Hashtags as csv for Excel
> write.csv(htags, "ht_unsort_danang.csv", fileEncoding = "UTF-8")


If anyone of you see when is the reason, please help me to solve it. Thank a lots

Here is a part of my data
ID Post_URL Img_URL Likes Owner Text Date
1 B06M_b7HSkD http://instagram.com/p/B06M_b7HSkD https://instagram.ficn1-1.fna.fbcdn.net/vp/0d90e0fb76b8847a783c3c5cb6095a39/5D4F5442/t51.2885-15/e35/66002370_1398344343637083_3615835770156874218_n.jpg?_nc_ht=instagram.ficn1-1.fna.fbcdn.net 0 2177301902 #danang 8/9/2019 1:18
2 B06M1jiH_xP http://instagram.com/p/B06M1jiH_xP https://instagram.ficn1-1.fna.fbcdn.net/vp/af784870125002b1aa6a617ab684dc4b/5DD5E347/t51.2885-15/e35/67410964_732825710483842_3310018650073794792_n.jpg?_nc_ht=instagram.ficn1-1.fna.fbcdn.net 2 6927674618 From handsome bridges to abandoned theme parks <U+0001F1FB><U+0001F1F3> #handsbridge #banahills #themepark #giants #thuytien #abandonedplaces #spooky #vietnam #danang #hue #travelasia #backpackerlife 8/9/2019 1:16
3 B06MoSbjs_d http://instagram.com/p/B06MoSbjs_d https://instagram.ficn1-1.fna.fbcdn.net/vp/1e75decd2da1c31dc91a8a3cc4a2edbd/5DE5CD94/t51.2885-15/e35/66623799_1227755864064850_909673923718068609_n.jpg?_nc_ht=instagram.ficn1-1.fna.fbcdn.net 4 11212777904 @hoianroasteryvietnam  #girlwholuv2eat 8/9/2019 1:15
4 B06MlfcI1jB http://instagram.com/p/B06MlfcI1jB https://instagram.ficn1-1.fna.fbcdn.net/vp/ef145be39e3c8ad00e02125a68e4d985/5DE97B16/t51.2885-15/e35/s1080x1080/67033031_2489069064484661_4117064706369827748_n.jpg?_nc_ht=instagram.ficn1-1.fna.fbcdn.net 4 176049633 Da Nang beach #danang#beach#vietnam 8/9/2019 1:14
5 B06MesFDZJP http://instagram.com/p/B06MesFDZJP https://instagram.ficn1-1.fna.fbcdn.net/vp/bf2ff6302429a7be12cf5ea75ec9c9f0/5DC9E4F6/t51.2885-15/e35/s1080x1080/66056130_543544639516171_6650088218280876115_n.jpg?_nc_ht=instagram.ficn1-1.fna.fbcdn.net 4 6737516 We spent five days at this beautiful city without any plan, wandering around the city, go to wherever the kids like or even spent a morning at a little cafe chitchatting and these all made a perfect memories <U+2764><U+FE0F> #danang #summerwithkids #hkmum #titiara #lovelytime #happysummer #summerbreak #beautifulday #happinessfound #bondingtime 8/9/2019 1:13
6 B06MPgontHx http://instagram.com/p/B06MPgontHx https://instagram.ficn1-1.fna.fbcdn.net/vp/a00ac2964c570839e1c219608fa3a7ac/5DE2062C/t51.2885-15/e35/p1080x1080/66488860_344250433185196_6531878865980847865_n.jpg?_nc_ht=instagram.ficn1-1.fna.fbcdn.net 4 400071369 Hi #Danang, I’ve missed you and your beautiful skies! (I didn’t miss the blistering heat though!) 8/9/2019 1:11
7 B06MOVTBzxl http://instagram.com/p/B06MOVTBzxl https://instagram.ficn1-1.fna.fbcdn.net/vp/59e89c84c240c38c361f631dd910ea78/5DDEF035/t51.2885-15/e35/67746501_1089110771289368_8317240515519625707_n.jpg?_nc_ht=instagram.ficn1-1.fna.fbcdn.net 7 3483822 Following what was probably the longest travel "day" of my life which played out like a game of chutes and ladders the end was nearly in sight as our plane approached Da Nang only to be sent into a holding pattern amid dark and stormy weather. Finally, when it was our turn to land we descended below the clouds and were greeted with this mind-blowing sunset. . . . <U+0001F4CD>Da Nang area, Vietnam | August 8, 2019 . . . #sunset #cloudporn #skyporn #sunsetchaser #upabove #aerialphotography #aerialbeauty #dramatic #lightrays #vietnam #danang #passionpassport #exploretocreate #wanderlust #mytravelgram #galaxys10e #luminar #asia #weather 8/9/2019 1:11
8 B06MNwep7TF http://instagram.com/p/B06MNwep7TF https://instagram.ficn1-1.fna.fbcdn.net/vp/a1bfb575817835c86109edd89e03a7a8/5DED2B50/t51.2885-15/e35/p1080x1080/66279516_168787590829741_3227800686880879360_n.jpg?_nc_ht=instagram.ficn1-1.fna.fbcdn.net 2 815599745 Like Father like Son <U+2764><U+FE0F> #trip #danang #summer <U+0001F334> 8/9/2019 1:11
9 B06MMKaDjO8 http://instagram.com/p/B06MMKaDjO8 https://instagram.ficn1-1.fna.fbcdn.net/vp/5d5d170c57e1a54de96839837b091c92/5DD02B2A/t51.2885-15/e35/66765690_184511239230700_8399687871950705737_n.jpg?_nc_ht=instagram.ficn1-1.fna.fbcdn.net 6 11212777904 @hoianroasteryvietnam  #girlwholuv2eat 8/9/2019 1:11
10 B06LYnUHfgo http://instagram.com/p/B06LYnUHfgo https://instagram.ficn1-1.fna.fbcdn.net/vp/193ac0ce81440391768d227ca69b60f4/5DE23313/t51.2885-15/e35/s1080x1080/66121653_866725717034121_7051459755409878847_n.jpg?_nc_ht=instagram.ficn1-1.fna.fbcdn.net 2 2956363480 Sammy knows how to walk through the hell <U+0001F914><U+0001F914><U+0001F914> #danang #vietnam 8/9/2019 1:04
11 B0Tg-8-nSve http://instagram.com/p/B0Tg-8-nSve https://instagram.ficn1-1.fna.fbcdn.net/vp/d84f3b0cfa7f4c1ed27fa5394797b32e/5DCD1F00/t51.2885-15/e35/s1080x1080/67400404_659355874538483_6067415783466168554_n.jpg?_nc_ht=instagram.ficn1-1.fna.fbcdn.net 125 45002590 But, don’t they know that it is in our hands to hold our happiness? We always have chances and choices. . . . #couple  #love  #life  #happy  #iloveyou  #gymboy  #gymgirl  #gymlife  #strong  #fitness  #gym  #fit  #together  #forever  #photooftheday  #picoftheday  #ootd  #instagood  #couplegoals  #instalove  #hubby  #lovequotes  #quotes  #ootd  #instatravel  #danang 7/25/2019 0:42
12 B06JNI4hLse http://instagram.com/p/B06JNI4hLse https://instagram.ficn1-1.fna.fbcdn.net/vp/417d4a43c1c04cb27613ddee4197e370/5DCDA472/t51.2885-15/e35/s1080x1080/66364041_1148973645289093_7855565106563805368_n.jpg?_nc_ht=instagram.ficn1-1.fna.fbcdn.net 4 1356108817 Hoiana Casino family wishes our GM Mr. Alex Khoo a very Happy Birthday.  We wish that all your dreams will come true soon and we heard it loud & clear... So we’ll make it happen! #birthday #birthdayboss #hoianacasino #casino #danang #quangnam 8/9/2019 0:45

Charles Afful

unread,
Mar 2, 2020, 7:23:30 PM3/2/20
to unmarked
i have an inflation data and am trying to find the transition matrix if it and am having problem with the code am using this is the code i used 
tm=matrix(c(rep(0,54)),3,3);tm
tpm=matrix(c(rep(0,54)),3,3);tpm
for(i in 1:length(data)-1)
tm[da[i],da[i+1]]=(tm[da[i],da[i+1]]+1)
tm
for (j in 1:3) 
  tpm[j,]=(tm[j,]/sum(tm[j,]))
tpm  

and then also what the codes to find transition frequency

Audace ADANTODE

unread,
Jun 4, 2020, 3:54:00 PM6/4/20
to unmarked
Hello Florent

I am facing the same problem. Here is my data and the code I used.

rm(list=ls())
library(raster)
library(ncdf4)
library(ncdf.tools)
library(ncdfgeom)
library(rgdal)
library(gdata)

setwd("C:/Users/USER/Desktop/spmo/Précipitation/2018")
getwd()

MergeBIL2NC<- function(variable,year){
  pattern <- paste("PRISM_",variable,"_stable_4kmD1_",year,".*.bil$",sep="")
  cat(paste("pattern, ", pattern,"!\n",sep="",collapse=""))
  files <- list.files(path=".",pattern=pattern,recursive=TRUE,full.names=TRUE)
  cat(paste("filelist, ", files,"!\n",sep="",collapse=""))
  t <- stack(files)
  filename <- paste(variable,"_",year,".nc",sep="")
  cat(paste("filename, ", filename,"!\n",sep="",collapse=""))
  t_nc <- writeRaster(t,filename=filename,bandorder="BIL",overwrite=TRUE,format="CDF",varname=variable)}

MergeBIL2NC("ppt",2018)


May you help me?

Poorvaja Ganesan

unread,
Jul 27, 2020, 11:03:26 AM7/27/20
to unmarked
Hi 

I am new to r programming and I dunno exactly where is my data set. I have pasted the code I tried and the error I got. Please help me out. 
install.packages("Seurat")
library(Seurat)
library(ggplot2)
library(sctransform)

Read10X(data.dir = NULL, gene.column = 2, unique.features = TRUE, strip.suffix = FALSE)
pbmc_data <- Read10X(data.dir = "../data/pbmc3k/filtered_gene_bc_matrices/hg19/")
pbmc <- CreateSeuratObject(counts = pbmc_data)
# Lets examine a few genes in the first thirty cells
pbmc.data[c("CD3D", "TCL1A", "MS4A1"), 1:30]


On Friday, October 21, 2016 at 8:47:32 AM UTC-4, Ias S wrote:
Reply all
Reply to author
Forward
0 new messages