Quantcast

replacement has length zero

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

replacement has length zero

fabiano
I have been working on the following code but keep getting an err message. My current thinking is that the problem is on the indexing but do not know how to fix it. Any help please?

ungulate <- read.csv("Ungulate.csv",row.names=1)
ungulate <- as.matrix(ungulate);colnames(ungulate)<-NULL;rownames(ungulate)<-NULL
habitat <- read.csv("Ungulate_vegetation.csv")
habitat <- habitat[,3]
site.data <- read.csv("Ungulate_site.csv")
site <- site.data$SiteId
visit <- site.data$Visit
date <- site.data$Date
date <- matrix(date,nrow=20,ncol=7,byrow=TRUE)
S <- dim(ungulate)[1]
m <- 14 # number of augmented species
G <- read.csv("Ungulate_group.csv")
G <- G[,2]
g <- rep(NA,length=m)
G <- c(G,g) # combined list with actual and augmented data set so in this case 6 + 14
g <- length(table(G))
# habitat id
Hab <- cbind(seq(1,20),habitat) #habitat types for 20 sties run from 2 to 4

hab <- matrix(NA,nrow=2,ncol=7)    #creating placing values to be filled with values generated by the following loop
for(i in 1:4){
        hab[i,] <- Hab[habitat==i,1]   }   # want to use this loop to substitute NA by exact values as those found in the real data set which can be 2 to 4 but keep getting an error message saying

Error in hab[i, ] <- Hab[habitat == i, 1] : replacement has length zero

Any help. Thanks
Error in hab[i, ] <- Hab[habitat == i, 1] : replacement has length zero
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: replacement has length zero

Jeff Newmiller
Your request fails the reproducibility requirement from the posting guide, because you have not supplied enough data to run the code and reproduce the error.
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<[hidden email]>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
---------------------------------------------------------------------------
Sent from my phone. Please excuse my brevity.

fabiano <[hidden email]> wrote:

>I have been working on the following code but keep getting an err
>message. My
>current thinking is that the problem is on the indexing but do not know
>how
>to fix it. Any help please?
>
>ungulate <- read.csv("Ungulate.csv",row.names=1)
>ungulate <-
>as.matrix(ungulate);colnames(ungulate)<-NULL;rownames(ungulate)<-NULL
>habitat <- read.csv("Ungulate_vegetation.csv")
>habitat <- habitat[,3]
>site.data <- read.csv("Ungulate_site.csv")
>site <- site.data$SiteId
>visit <- site.data$Visit
>date <- site.data$Date
>date <- matrix(date,nrow=20,ncol=7,byrow=TRUE)
>S <- dim(ungulate)[1]
>m <- 14 # number of augmented species
>G <- read.csv("Ungulate_group.csv")
>G <- G[,2]
>g <- rep(NA,length=m)
>G <- c(G,g) # combined list with actual and augmented data set so in
>this
>case 6 + 14
>g <- length(table(G))
># habitat id
>Hab <- cbind(seq(1,20),habitat) #habitat types for 20 sties run from 2
>to 4
>
>hab <- matrix(NA,nrow=2,ncol=7)    #creating placing values to be
>filled
>with values generated by the following loop
>for(i in 1:4){
> hab[i,] <- Hab[habitat==i,1]   }   # want to use this loop to
>substitute NA
>by exact values as those found in the real data set which can be 2 to 4
>but
>keep getting an error message saying
>
>Error in hab[i, ] <- Hab[habitat == i, 1] : replacement has length zero
>
>Any help. Thanks
>Error in hab[i, ] <- Hab[habitat == i, 1] : replacement has length zero
>
>--
>View this message in context:
>http://r.789695.n4.nabble.com/replacement-has-length-zero-tp4635700.html
>Sent from the R help mailing list archive at Nabble.com.
>
>______________________________________________
>[hidden email] mailing list
>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.

______________________________________________
[hidden email] mailing list
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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: replacement has length zero

Peter Ehlers
In reply to this post by fabiano
On 2012-07-07 03:19, fabiano wrote:

> I have been working on the following code but keep getting an err message. My
> current thinking is that the problem is on the indexing but do not know how
> to fix it. Any help please?
>
> ungulate <- read.csv("Ungulate.csv",row.names=1)
> ungulate <-
> as.matrix(ungulate);colnames(ungulate)<-NULL;rownames(ungulate)<-NULL
> habitat <- read.csv("Ungulate_vegetation.csv")
> habitat <- habitat[,3]
> site.data <- read.csv("Ungulate_site.csv")
> site <- site.data$SiteId
> visit <- site.data$Visit
> date <- site.data$Date
> date <- matrix(date,nrow=20,ncol=7,byrow=TRUE)
> S <- dim(ungulate)[1]
> m <- 14 # number of augmented species
> G <- read.csv("Ungulate_group.csv")
> G <- G[,2]
> g <- rep(NA,length=m)
> G <- c(G,g) # combined list with actual and augmented data set so in this
> case 6 + 14
> g <- length(table(G))
> # habitat id
> Hab <- cbind(seq(1,20),habitat) #habitat types for 20 sties run from 2 to 4
>
> hab <- matrix(NA,nrow=2,ncol=7)    #creating placing values to be filled
> with values generated by the following loop
> for(i in 1:4){
> hab[i,] <- Hab[habitat==i,1]   }   # want to use this loop to substitute NA
> by exact values as those found in the real data set which can be 2 to 4 but
> keep getting an error message saying
>
> Error in hab[i, ] <- Hab[habitat == i, 1] : replacement has length zero

Have you taken a look at just what 'Hab' and 'habitat' contain?
It looks to me that 'habitat' is not a set of integers which it
would have to be to get habitat==i to be TRUE. I guess that
Hab[habitat==i, 1] is the empty set (hence 'length zero').

Peter Ehlers

>
> Any help. Thanks
> Error in hab[i, ] <- Hab[habitat == i, 1] : replacement has length zero
>
> --
> View this message in context: http://r.789695.n4.nabble.com/replacement-has-length-zero-tp4635700.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list
> 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.
>

______________________________________________
[hidden email] mailing list
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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: replacement has length zero

fabiano
Thanks Peter.

We had a look at both Hab and habitat. These are integers representing habitat types.

habitat <- read.csv("Ungulate_vegetation.csv")
habitat <- habitat[,3]
habitat
[1] 3 3 4 3 3 3 4 4 3 3 3 3 3 4 2 3 2 3 2 3

Hab <- cbind(seq(1,20),habitat)
Hab
         habitat
 [1,]  1       3
 [2,]  2       3
 [3,]  3       4
 [4,]  4       3
 [5,]  5       3
 [6,]  6       3
 [7,]  7       4
 [8,]  8       4
 [9,]  9       3
[10,] 10       3
[11,] 11       3
[12,] 12       3
[13,] 13       3
[14,] 14       4
[15,] 15       2
[16,] 16       3
[17,] 17       2
[18,] 18       3
[19,] 19       2
[20,] 20       3

hab
     [,1] [,2] [,3] [,4] [,5] [,6] [,7]
[1,]   NA   NA   NA   NA   NA   NA   NA
[2,]   NA   NA   NA   NA   NA   NA   NA

for(i in 1:2){ hab[i, ] <- Hab[habitat==i,1] }

The idea is to assign habitat types to hab.

Thanks
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: replacement has length zero

Peter Ehlers
On 2012-07-08 06:57, fabiano wrote:

> Thanks Peter.
>
> We had a look at both Hab and habitat. These are integers representing
> habitat types.
>
> habitat <- read.csv("Ungulate_vegetation.csv")
> habitat <- habitat[,3]
> habitat
> [1] 3 3 4 3 3 3 4 4 3 3 3 3 3 4 2 3 2 3 2 3
>
> Hab <- cbind(seq(1,20),habitat)
> Hab
>           habitat
>   [1,]  1       3
>   [2,]  2       3
>   [3,]  3       4
>   [4,]  4       3
>   [5,]  5       3
>   [6,]  6       3
>   [7,]  7       4
>   [8,]  8       4
>   [9,]  9       3
> [10,] 10       3
> [11,] 11       3
> [12,] 12       3
> [13,] 13       3
> [14,] 14       4
> [15,] 15       2
> [16,] 16       3
> [17,] 17       2
> [18,] 18       3
> [19,] 19       2
> [20,] 20       3
>
> hab
>       [,1] [,2] [,3] [,4] [,5] [,6] [,7]
> [1,]   NA   NA   NA   NA   NA   NA   NA
> [2,]   NA   NA   NA   NA   NA   NA   NA
>
> for(i in 1:2){ hab[i, ] <- Hab[habitat==i,1] }
>
> The idea is to assign habitat types to hab.

I don't understand how this relates to a 2-by-7 matrix,
but it's easy to see where your problem with the loop is.
Try this and see if it sheds light:

   hab[1,] <- Hab[habitat==1,1]
   #Error in hab[1, ] <- Hab[habitat == 1, 1] : replacement has length zero

   hab[2,] <- Hab[habitat==2,1]
   #Error in hab[2, ] <- Hab[habitat == 2, 1] :
   number of items to replace is not a multiple of replacement length

Now do:
   Hab[habitat==1,1]    ## how many 1s are in habitat?
   Hab[habitat==2,1]    ## how many 2s are in habitat?

I think that you may have to rethink your approach.

Peter Ehlers

>
> Thanks
>
> --
> View this message in context: http://r.789695.n4.nabble.com/replacement-has-length-zero-tp4635700p4635777.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list
> 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.
>

______________________________________________
[hidden email] mailing list
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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: replacement has length zero

fabiano
In reply to this post by fabiano
Thanks Peter.  

We did manage to solve the problem using the approach bellow.

Hab <- cbind(seq(1,21),habitat)
hab <- matrix(NA,nrow=3,ncol=7)
for (i in 1:3) {hab[i,] <- Hab[habitat==i,1]}

The problem was that the hab matrix was not a multiple of habitat.

 
Loading...