Re: [R] Error in as.xts

2012-07-16 Thread Rui Barradas

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 = order.by, frequency = frequency, ...) :
   NROW(x) must match length(order.by)
Here is how the data looks like

d1 - read.csv(file.path(dataDir,AppendixA-FishCountsTable-2009.csv),

as.is=T)

d1[1:3,]

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

diveData_2009 - as.xts( d1,order.by=as.POSIXct(strptime(paste(d$date,

d$TIME ), %d/%m/%Y %H:%M) ))
Error in xts(x, order.by = order.by, frequency = frequency, ...) :
   NROW(x) must match length(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 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.



__
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.


Re: [R] Error in as.xts

2012-07-16 Thread Rui Barradas

Hello,

I'm glad it helped.

Your attachment came in corrupted. It's better to save the file as csv 
or tab or space separated values text file. Use write.table or write.csv 
to save the data. Even if there's a transmission problem, most of it 
will be intact, just delete a row or so.


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() : NAs introduced by coercion

2: In as_numeric() : 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 sizesite
depth level
TRANSECT VIS_M
1  62 10/12/2009 12:44 E. lateralis 2   15 Hopkins15
 B
 1 4
2  62 10/12/2009 12:44 E. lateralis 1   22 Hopkins15
 B
 1 4
3  62 10/12/2009 12:44 E. lateralis 1   25 Hopkins15
 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.


Re: [R] Error in as.xts

2012-07-16 Thread Rui Barradas

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() : NAs introduced by coercion

2: In as_numeric() : 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 sizesite
depth level
TRANSECT VIS_M
1  62 10/12/2009 12:44 E. lateralis 2   15 Hopkins15
 B
 1 4
2  62 10/12/2009 12:44 E. lateralis 1   22 Hopkins15
 B

Re: [R] Error in as.xts

2012-07-16 Thread Rui Barradas

Hello,

You'll never get a df with 97 rows with that dataset. Just look.
(The last dive_id is 97 but it starts at 62, not at 1.)

dat - read.csv(diveData_2009.csv, header=TRUE, stringsAsFactors=FALSE)
#
DateTime - with(dat, paste(date, time))
DateTime - as.POSIXct(DateTime, format=%m/%d/%Y %H:%M)
diveData_2009 - xts(dat ,order.by=DateTime)

# Result shall not have 97 rows
uinx_2009 - unique(as.character(index(diveData_2009)))
length(uinx_2009) # 27
uid_2009 - unique(diveData_2009$dive_id)
length(uid_2009) # 36

# A solution
fun - function(x){
dd - with(x, paste(date[1], time[1]))
dd - as.POSIXct(dd, format = %m/%d/%Y %H:%M)
id - x$dive_id[1]
tt - x$TRANSECT[1]
data.frame(dive_id=id, timestamp=dd, TRANSECT=tt)
}
bb - by(dat, dat$dive_id, FUN = fun)
diveCond_2009 - do.call(rbind, bb)


Then add columns to this result:

diveCond_2009$r_wvht - 0

Etc.

Rui Barradas

Em 16-07-2012 19:46, Yolande Tra escreveu:

Yes the error happens for the 2009 file (attached). The dive_id in
diveCond stops at dive_id=88 even though the last dive_id in 2009 file
is dive_id=97
diveCond - data.frame(matrix(0, nrow=97, 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])

}
for(i in unique(as.character(index(diveData_2009{
  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_2009[i][1,11])

}


On Mon, Jul 16, 2012 at 2:32 PM, Rui Barradas ruipbarra...@sapo.pt
mailto:ruipbarra...@sapo.pt wrote:

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 http://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 

[R] Error in as.xts

2012-07-15 Thread Yolande Tra
Hi
I got the following error using as.xts
Error in xts(x, order.by = order.by, frequency = frequency, ...) :
  NROW(x) must match length(order.by)
Here is how the data looks like
 d1 - read.csv(file.path(dataDir,AppendixA-FishCountsTable-2009.csv),
as.is=T)
 d1[1:3,]
  dive_id   date  time  species count sizesite depth level
TRANSECT VIS_M
1  62 10/12/2009 12:44 E. lateralis 2   15 Hopkins15 B
   1 4
2  62 10/12/2009 12:44 E. lateralis 1   22 Hopkins15 B
   1 4
3  62 10/12/2009 12:44 E. lateralis 1   25 Hopkins15 B
   1 4
 diveData_2009 - as.xts( d1,order.by=as.POSIXct(strptime(paste(d$date,
d$TIME ), %d/%m/%Y %H:%M) ))
Error in xts(x, order.by = order.by, frequency = frequency, ...) :
  NROW(x) must match length(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 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.