[R] need help please

arun smartpink111 at yahoo.com
Wed Jul 18 01:46:29 CEST 2012


Hi Yolande,

For your first error regaring factor, I am getting a single number

thisIndex <- as.character(index(diveData_2008[2,]))
> dive_id <- diveCond_all$dive_id[diveCond_all$timestamp==thisIndex]
> dive_id
[1] 1
#because there is only one TRUE value below and that refers to dive_id 1.


diveCond_all$dive_id
 [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
[26] 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
[51] 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
[76] 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
> diveCond_all$timestamp==thisIndex
 [1]  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[13] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[25] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[37] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[49] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[61] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[73] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[85] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[97] FALSE

#Here I am using only diveData_2008

N <- dim(diveData_2008)[1]
#Here I found a mistake.  I guess the ncol should be 10 instead of 9.  Also, names(fishReport)  has 9 columns where it should be 10 because in the loop 2008, it has 10 columns to be replaced.  I might be wrong as I am a bit busy heading towards home.  Will look into it later.


fishReport <- data.frame(matrix(0, nrow=N, ncol=10))
names(fishReport) <- c("dive_id", "site", "section", "level", "transect", "depth", "species", "count", "size","visibility")

#Now the loop part is a bit baffling.  

## The 2008 dives

#Here, dive_id has 97 elements.  It should be replaced by fishReport$dive_id[i] containing 674 elements.  I guess that is where the error shows.  So, I am just commenting that line #and running.  It shows no errors.  
for(i in 1:dim(diveData_2008)[1]){
                # Which dive is this observation from
                thisIndex <- as.character(index(diveData_2008[i,]))
                dive_id <- diveCond_all$dive_id[diveCond_all$timestamp==thisIndex]
                cat(dive_id, thisIndex, "\n")
                # Store the pertinent data in the fish report data.frame
                #fishReport$dive_id[i]     <- dive_id
                fishReport$site[i]       <- diveData_2008[i,"site"]
                fishReport$level[i]     <- diveData_2008[i,"level"]
                fishReport$section[i]     <- 0
                fishReport$transect[i]   <- as.numeric(diveData_2008[i,"TRANSECT"])
                fishReport$depth[i]       <- as.numeric(diveData_2008[i,"depth"])
               
 fishReport$species[i]     <- 
fishTable$name_2009[fishTable$name_2008==as.character(diveData_2008[i,"species"])
 ][1]
                fishReport$count[i]       <- as.numeric(diveData_2008[i,"count"])
                fishReport$size[i]       <- as.numeric(diveData_2008[i,"size"])
      fishReport$visibility[i]         <- as.numeric(diveData_2008[i,"VIS_M"])
}

 head(fishReport)
  dive_id    site section level transect depth species count size visibility
1       1 Hopkins       0     B        1    15      NA    15    6        3.5
2       1 Hopkins       0     B        1    15      NA     1   23        3.5
3       1 Hopkins       0     B        1    15      NA     1   27        3.5
4       1 Hopkins       0     B        1    15      NA     1   29        3.5
5       1 Hopkins       0     B        1    15      NA     1    8        3.5
6       1 Hopkins       0     B        1    15      NA     1    8        3.5
> 
tail(fishReport)
    dive_id        site section level transect depth species count size
669       0 Point Pinos       0     B        1    15      NA     2   30
670       0 Point Pinos       0     B        1    15      NA     1    7
671       0 Point Pinos       0     B        1    15      NA     1    8
672       0 Point Pinos       0     M        1    15      NA     1   18
673       0 Point Pinos       0     B        2    10      NA     1   42
674       0 Point Pinos       0     M        2    10      NA    NA   NA
    visibility
669        3.0
670        3.0
671        3.0
672        3.0
673        2.5
674        2.5

Still has to sort out the dive_id.

WIll check it later.

I am copying this to Rhelp.  Probably somebody else will find the errors better than me.

A.K.






________________________________
From: Yolande Tra <yolande.tra at gmail.com>
To: arun <smartpink111 at yahoo.com> 
Sent: Tuesday, July 17, 2012 5:23 PM
Subject: need help please


Hi Arun,

I am still impressed by the way you solved my questions. I have a last one. Please bear with me. Attached are four files used in the following codes. There are two parts in the code.

First part
I pickedup i=2 for the code 
thisIndex <- as.character(index(diveData_2008[i,]))

> thisIndex <- as.character(index(diveData_2008[2,]))
> dive_id <- diveCond_all$dive_id[diveCond_all$timestamp==thisIndex]
> dive_id
factor(0)
97 Levels:  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 ... 97
It gives a factor instead of a single number: dive_id

dive_id <- diveCond_all$dive_id[diveCond_all$timestamp==thisIndex]
cat(dive_id, thisIndex, "\n")
fishReport$dive_id[i]     <- dive_id

The program at the end of part 1 says 
2008-08-06 08:49:00 
Error in fishReport$dive_id[i] <- dive_id : replacement has length zero

Second part
It gives the same type of error around dive_id
 2009-10-12 12:44:00 
Error in fishReport$dive_id[i + j] <- dive_id : 
  replacement has length zero



N <- dim(diveData_2009)[1] + dim(diveData_2008)[1]
fishReport <- data.frame(matrix(0, nrow=N, ncol=9))
names(fishReport) <- c("dive_id", "site", "section", "level", "transect", "depth", "species", "count", "size")

## The 2008 dives
for(i in 1:dim(diveData_2008)[1]){
                # Which dive is this observation from
                thisIndex <- as.character(index(diveData_2008[i,]))
                dive_id <- diveCond_all$dive_id[diveCond_all$timestamp==thisIndex]
                cat(dive_id, thisIndex, "\n")
                # Store the pertinent data in the fish report data.frame
                fishReport$dive_id[i]     <- dive_id
                fishReport$site[i]                             <- diveData_2008[i,"site"]
                fishReport$level[i]                          <- diveData_2008[i,"level"]
                fishReport$section[i]     <- 0
                fishReport$transect[i]   <- as.numeric(diveData_2008[i,"TRANSECT"])
                fishReport$depth[i]                        <- as.numeric(diveData_2008[i,"depth"])
                fishReport$species[i]     <- fishTable$name_2009[fishTable$name_2008==as.character(diveData_2008[i,"species"]) ][1]
                fishReport$count[i]                        <- as.numeric(diveData_2008[i,"count"])
                fishReport$size[i]                            <- as.numeric(diveData_2008[i,"size"])
      fishReport$visibility[i]                              <- as.numeric(diveData_2008[i,"VIS_M"])
}
j<-i
## The 2009 dives
for(i in i+1:dim(diveData_2009)[1]){
                thisIndex <- as.character(index(diveData_2009[i,]))
                dive_id <- diveCond_all$dive_id[diveCond_all$timestamp==thisIndex]
                cat(dive_id, thisIndex, "\n")       
                fishReport$dive_id[i+j]                 <- dive_id
                fishReport$site[i+j]                         <- "Hopkins"
                fishReport$level[i+j]                      <- diveData_2009[i,"level"]
                fishReport$transect[i+j]               <- as.numeric(diveData_2009[i,"TRANSECT"])
                fishReport$depth[i+j]                                    <- as.numeric(diveData_2009[i,"depth"])
                fishReport$species[i+j]                 <- diveData_2009[i,"species"]
                fishReport$count[i+j]                    <- as.numeric(diveData_2008[i,"count"])
                fishReport$size[i+j]                        <- as.numeric(diveData_2009[i,"size"])
      fishReport$visibility[i+j]      <- as.numeric(diveData_2008[i,"VIS_M"])
                }

Thank you,
Yolande         



More information about the R-help mailing list