Hello,

I couldn't reproduce your error, with the csv file, your code run at the first try. Here is exactly what I've done



dat <- read.csv("diveData_2008.csv", header=TRUE, stringsAsFactors=FALSE)

# This 'DateTime' will be used again later.
DateTime <- with(dat, paste(date, time))
DateTime <- as.POSIXct(DateTime, format="%m/%d/%Y %H:%M")

# Create the xts object, slightly different, [ not as.xts() ]
diveData_2008 <- xts(dat ,order.by=DateTime)

# Your code
diveCond <- data.frame(matrix(0, nrow=61, ncol=17))
names(diveCond) <- c("dive_id", "timestamp", "visability", "r_wvht",
        "r_dpd", "r_apt", "r_mwd", "r_wtmp", "l_salinity", "l_o2",
        "l_hs", "l_tp", "l_wdir", "l_along", "l_cross", "l_mab",
        "l_depth")
dive_id <- 0

for(i in unique(as.character(index(diveData_2008)))){
    dive_id <- dive_id+1
    diveCond$dive_id[dive_id] <- dive_id
    diveCond$timestamp[dive_id] <- as.character(i)
    diveCond$visability[dive_id] <- as.numeric(diveData_2008[i][1,11])
}

# See ?aggregate and note that the function is
# is FUN = `[`with further args 1 (the dots '...')
# to extract the first row in each sub-df
aggr <- aggregate(TRANSECT ~ index(diveData_2008), data = diveData_2008, `[`, 1)
names(aggr)[1] <- "timestamp"
merge(data.frame(timestamp=unique(DateTime)), aggr, all.x=TRUE)


The output of this merge() is similar to your diveCond, but with much less columns, it only has the columns with values different from zero.

The aggregate alone wouldn't include the <NA> value, only the ones with an actual timestamp.

Anyway, there was no error in the creation of diveCond. This leads me to the final note. If something is going wrong break the instructions nto smaller simpler ones. See, for instance, how I've broken the DateTime variable, without putting everything in the as.POSIXct, just the end value after paste. Though the possible error wasn't there, it's allways better to debug simpler code, EVEN if at the cost of making longer, with more lines.

Hope this helps,

Rui Barradas

Em 16-07-2012 15:27, Yolande Tra escreveu:
Hello,
Can I ask you another question? Attached is the file created from
as.xts. After submitting the following code, I got an error. Please
help. Thanks.

diveCond <- data.frame(matrix(0, nrow=61, ncol=17))

names(diveCond) <- c("dive_id", "timestamp", "visability", "r_wvht",
"r_dpd", "r_apt", "r_mwd", "r_wtmp", "l_salinity", "l_o2", "l_hs",
"l_tp", "l_wdir", "l_along", "l_cross", "l_mab", "l_depth")

dive_id <- 0

for(i in unique(as.character(index(diveData_2008)))){

dive_id <- dive_id+1

diveCond$dive_id[dive_id] <- dive_id

diveCond$timestamp[dive_id] <- as.character(i)

diveCond$visability[dive_id] <- as.numeric(diveData_2008[i][1,11])

}

Error in if (length(c(year, month, day, hour, min, sec)) == 6 && c(year,:

missing value where TRUE/FALSE needed

In addition: Warning messages:

1: In as_numeric(YYYY) : NAs introduced by coercion

2: In as_numeric(YYYY) : NAs introduced by coercion

Thanks,

Yolande



On Mon, Jul 16, 2012 at 2:51 AM, Rui Barradas <ruipbarra...@sapo.pt
<mailto:ruipbarra...@sapo.pt>> wrote:

    Hello,

    I can see several things that are not right or may go wrong.
    (Without an actual dataset, this is just a series of hints.)

    1. The read.csv statement. Like read.table, it creates a data.frame,
    which defaults to reading strings as factors, coded internally as
    integers. Sometimes there are problems, when using them and you are
    converting the dates/times to POSIXct. You can try to use read.csv
    option

    stringsAsFactors = FALSE

    and then do the conversions. These conversions would include
    converting 'species' and 'site' to factor, if you want them as factors.

    2. Are there typos in your posted code example?
    2.a) You read into data.frame 'd1' but then use d$date and d$TIME.
    2.b) In 'd1' the column is named 'time', not 'TIME'.

    3. Try to create the date/time and assign it to a variable, for
    instance,

    dtvar <- as.POSIXct(...etc...)

    Now you can check length(dtvar) and NROW(d1) to see if they are
    equal. You can also see if the conversion was allright. This is a
    general rule: if there's an error in a complicated instruction,
    break it into smaller ones.

    Hope this helps,

    Rui Barradas

    Em 16-07-2012 03:10, Yolande Tra escreveu:

        Hi
        I got the following error using as.xts
        Error in xts(x, order.by <http://order.by/> = order.by
        <http://order.by/>, frequency = frequency, ...) :
            NROW(x) must match length(order.by <http://order.by/>)
        Here is how the data looks like

            d1 <-
            
read.csv(file.path(dataDir,"__AppendixA-FishCountsTable-__2009.csv"),

        as.is <http://as.is/>=T)

            d1[1:3,]

            dive_id       date  time      species count size    site
        depth level
        TRANSECT VIS_M
        1      62 10/12/2009 12:44 E. lateralis     2   15 Hopkins    15
             B
             1     4
        2      62 10/12/2009 12:44 E. lateralis     1   22 Hopkins    15
             B
             1     4
        3      62 10/12/2009 12:44 E. lateralis     1   25 Hopkins    15
             B
             1     4

            diveData_2009 <- as.xts( d1,order.by
            <http://order.by/>=as.POSIXct(__strptime(paste(d$date,

        d$TIME ), "%d/%m/%Y %H:%M") ))
        Error in xts(x, order.by <http://order.by/> = order.by
        <http://order.by/>, frequency = frequency, ...) :
            NROW(x) must match length(order.by <http://order.by/>)

        I could not figure out how to correct it
        Thank you for your help
        Yolande

                 [[alternative HTML version deleted]]

        ________________________________________________
        R-help@r-project.org <mailto:R-help@r-project.org> mailing list
        https://stat.ethz.ch/mailman/__listinfo/r-help
        <https://stat.ethz.ch/mailman/listinfo/r-help>
        PLEASE do read the posting guide
        http://www.R-project.org/__posting-guide.html
        <http://www.r-project.org/posting-guide.html>
        and provide commented, minimal, self-contained, reproducible code.




______________________________________________
R-help@r-project.org 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 via email to