Re: [R] plotting multiple animal tracks against Date/Time
On Wed, Sep 29, 2010 at 7:52 AM, Struve, Juliane j.str...@imperial.ac.uk wrote: I will post the example again to see if its readable now. My question is why does read.zoo(file=filenames,) work and lapply(filenames, read.zoo,...) does not ? Since I am reading the same file in both statements I just do not know how to interpret Error in strptime(x, format, tz = tz) : invalid 'x' argument. Thank you for all help. Juliane library(chron) library(zoo) #Generate example file Fish_ID=1646 Date - 01/01/2004 00:01:00 Date - as.POSIXct(strptime(Date,format=%m/%d/%Y %H:%M:%S)) R2sqrt -100 Test - data.frame(Fish_ID=Fish_ID,Date=Date,R2sqrt=R2sqrt) write.csv(Test,file=Test) #Read in example file filenames=Test read.zoo(file=filenames, header = TRUE, FUN = as.chron, sep = ,, colClasses = c(NULL, NULL, character, numeric)) lapply(filenames, read.zoo, header = TRUE, FUN = as.chron, sep = ,, colClasses = c(NULL, NULL, character, numeric)) FUN is an argument of lapply so what is actually running is lapply(filenames, FUN = as.chron, ...) rather than lapply(filenames, FUN = read.zoo, ...). It seems the short form usage of lapply won`t work here. Try this instead: lapply(filenames, function(F) read.zoo(F, header = TRUE, sep = ,, FUN = as.chron, colClasses = c(NULL, NULL, character, numeric))) -- Statistics Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com __ 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] plotting multiple animal tracks against Date/Time
I will post the example again to see if its readable now. My question is why does read.zoo(file=filenames,) work and lapply(filenames, read.zoo,...) does not ? Since I am reading the same file in both statements I just do not know how to interpret Error in strptime(x, format, tz = tz) : invalid 'x' argument. Thank you for all help. Juliane library(chron) library(zoo) #Generate example file Fish_ID=1646 Date - 01/01/2004 00:01:00 Date - as.POSIXct(strptime(Date,format=%m/%d/%Y %H:%M:%S)) R2sqrt -100 Test - data.frame(Fish_ID=Fish_ID,Date=Date,R2sqrt=R2sqrt) write.csv(Test,file=Test) #Read in example file filenames=Test read.zoo(file=filenames, header = TRUE, FUN = as.chron, sep = ,, colClasses = c(NULL, NULL, character, numeric)) lapply(filenames, read.zoo, header = TRUE, FUN = as.chron, sep = ,, colClasses = c(NULL, NULL, character, numeric)) From: Gabor Grothendieck [ggrothendi...@gmail.com] Sent: 29 September 2010 00:09 To: Struve, Juliane Cc: r-help@r-project.org Subject: Re: [R] plotting multiple animal tracks against Date/Time On Tue, Sep 28, 2010 at 9:30 AM, Struve, Juliane j.str...@imperial.ac.uk wrote: Hi, in this self-contained example the file the same error message appears as when I read in my original results files. library (zoo) library(chron) #generate example data Fish_ID=1646 Date - 01/01/2004 00:01:00 Date - as.POSIXct(strptime(Date,format=%m/%d/%Y %H:%M:%S)) R2sqrt -100 #put into dataframe Test - data.frame(Fish_ID=Fish_ID,Date=Date,R2sqrt=R2sqrt) # write .csv file write.csv(Test,file=Test) #generate list of files filenames=Test #read file(s) into zoo object read.zoo(file=filenames, header = TRUE, FUN = as.chron, sep = ,, colClasses = c(NULL, NULL, character, numeric)) #works fine #read list of files into zoo.object lapply(filenames, read.zoo, header = TRUE, FUN = as.chron, sep = ,, colClasses = c(NULL, NULL, character, numeric))# error Error in strptime(x, format, tz = tz) : invalid 'x' argument Am I missing something ? Thank you for your time and patience. Self contained means anyone else can just copy your code and paste it into their session and see the error message you see. Its likely that your file does not contain what you think it does. -- Statistics Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com __ 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] plotting multiple animal tracks against Date/Time
Hi: This 'works' on the lapply end: # Function to read one file from the list: g - function(x) read.zoo(file = x, header = TRUE, FUN = as.chron, sep = ,, colClasses = c(NULL, NULL, character, numeric)) # Apply to all files in list: lapply(filenames, g) [[1]] (01/01/04 00:01:00) 100 # Just to prove to myself that this works on a list of input files, write.csv(Test,file=Test2) write.csv(Test,file=Test3) filenames - c('Test', 'Test2', 'Test3') lapply(filenames, g) [[1]] (01/01/04 00:01:00) 100 [[2]] (01/01/04 00:01:00) 100 [[3]] (01/01/04 00:01:00) 100 Hope this helps, Dennis On Wed, Sep 29, 2010 at 4:52 AM, Struve, Juliane j.str...@imperial.ac.ukwrote: I will post the example again to see if its readable now. My question is why does read.zoo(file=filenames,) work and lapply(filenames, read.zoo,...) does not ? Since I am reading the same file in both statements I just do not know how to interpret Error in strptime(x, format, tz = tz) : invalid 'x' argument. Thank you for all help. Juliane library(chron) library(zoo) #Generate example file Fish_ID=1646 Date - 01/01/2004 00:01:00 Date - as.POSIXct(strptime(Date,format=%m/%d/%Y %H:%M:%S)) R2sqrt -100 Test - data.frame(Fish_ID=Fish_ID,Date=Date,R2sqrt=R2sqrt) write.csv(Test,file=Test) #Read in example file filenames=Test read.zoo(file=filenames, header = TRUE, FUN = as.chron, sep = ,, colClasses = c(NULL, NULL, character, numeric)) lapply(filenames, read.zoo, header = TRUE, FUN = as.chron, sep = ,, colClasses = c(NULL, NULL, character, numeric)) From: Gabor Grothendieck [ggrothendi...@gmail.com] Sent: 29 September 2010 00:09 To: Struve, Juliane Cc: r-help@r-project.org Subject: Re: [R] plotting multiple animal tracks against Date/Time On Tue, Sep 28, 2010 at 9:30 AM, Struve, Juliane j.str...@imperial.ac.uk wrote: Hi, in this self-contained example the file the same error message appears as when I read in my original results files. library (zoo) library(chron) #generate example data Fish_ID=1646 Date - 01/01/2004 00:01:00 Date - as.POSIXct(strptime(Date,format=%m/%d/%Y %H:%M:%S)) R2sqrt -100 #put into dataframe Test - data.frame(Fish_ID=Fish_ID,Date=Date,R2sqrt=R2sqrt) # write .csv file write.csv(Test,file=Test) #generate list of files filenames=Test #read file(s) into zoo object read.zoo(file=filenames, header = TRUE, FUN = as.chron, sep = ,, colClasses = c(NULL, NULL, character, numeric)) #works fine #read list of files into zoo.object lapply(filenames, read.zoo, header = TRUE, FUN = as.chron, sep = ,, colClasses = c(NULL, NULL, character, numeric))# error Error in strptime(x, format, tz = tz) : invalid 'x' argument Am I missing something ? Thank you for your time and patience. Self contained means anyone else can just copy your code and paste it into their session and see the error message you see. Its likely that your file does not contain what you think it does. -- Statistics Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com __ 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. [[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.
Re: [R] plotting multiple animal tracks against Date/Time
Hi, in this self-contained example the file the same error message appears as when I read in my original results files. library (zoo) library(chron) #generate example data Fish_ID=1646 Date - 01/01/2004 00:01:00 Date - as.POSIXct(strptime(Date,format=%m/%d/%Y %H:%M:%S)) R2sqrt -100 #put into dataframe Test - data.frame(Fish_ID=Fish_ID,Date=Date,R2sqrt=R2sqrt) # write .csv file write.csv(Test,file=Test) #generate list of files filenames=Test #read file(s) into zoo object read.zoo(file=filenames, header = TRUE, FUN = as.chron, sep = ,, colClasses = c(NULL, NULL, character, numeric)) #works fine #read list of files into zoo.object lapply(filenames, read.zoo, header = TRUE, FUN = as.chron, sep = ,, colClasses = c(NULL, NULL, character, numeric))# error Error in strptime(x, format, tz = tz) : invalid 'x' argument Am I missing something ? Thank you for your time and patience. Juliane Sorry for posting twice, libraries were missing earlier. From: Gabor Grothendieck [ggrothendi...@gmail.com] Sent: 27 September 2010 23:32 To: Struve, Juliane Cc: r-help@r-project.org Subject: Re: [R] plotting multiple animal tracks against Date/Time On Mon, Sep 27, 2010 at 5:38 PM, Struve, Juliane j.str...@imperial.ac.uk wrote: Hello, thank you very much for replying. The code yields an error message Error in strptime(x, format, tz = tz) : invalid 'x' argument But I can't see what's wrong with it, the Date/Time info is in the third column, format is %Y-%m-%d %H:%M:%S. There is no time zone info in the data, could this be a problem ? See the last line on every message to r-help. You need to put together a minimal self contained example that illustrates the problem. The best I can do with the information provided is to point out that this works: Lines - ',Fish_ID,Date,R2sqrt + 1,1646,2006-08-18 08:48:59,0 + 2,1646,2006-08-18 09:53:20,100' library(zoo) library(chron) read.zoo(textConnection(Lines), header = TRUE, FUN = as.chron, + sep = ,, colClasses = c(NULL, NULL, character, numeric)) (08/18/06 08:48:59) (08/18/06 09:53:20) 0 100 -- Statistics Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com __ 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] plotting multiple animal tracks against Date/Time
On Tue, Sep 28, 2010 at 9:30 AM, Struve, Juliane j.str...@imperial.ac.uk wrote: Hi, in this self-contained example the file the same error message appears as when I read in my original results files. library (zoo) library(chron) #generate example data Fish_ID=1646 Date - 01/01/2004 00:01:00 Date - as.POSIXct(strptime(Date,format=%m/%d/%Y %H:%M:%S)) R2sqrt -100 #put into dataframe Test - data.frame(Fish_ID=Fish_ID,Date=Date,R2sqrt=R2sqrt) # write .csv file write.csv(Test,file=Test) #generate list of files filenames=Test #read file(s) into zoo object read.zoo(file=filenames, header = TRUE, FUN = as.chron, sep = ,, colClasses = c(NULL, NULL, character, numeric)) #works fine #read list of files into zoo.object lapply(filenames, read.zoo, header = TRUE, FUN = as.chron, sep = ,, colClasses = c(NULL, NULL, character, numeric))# error Error in strptime(x, format, tz = tz) : invalid 'x' argument Am I missing something ? Thank you for your time and patience. Self contained means anyone else can just copy your code and paste it into their session and see the error message you see. Its likely that your file does not contain what you think it does. -- Statistics Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com __ 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] plotting multiple animal tracks against Date/Time
On Mon, Sep 27, 2010 at 10:01 AM, Struve, Juliane j.str...@imperial.ac.uk wrote: Hi, I am sorry that my question wasn not very clearly formulated. My real data comes in 47 .csv files, one for each of 47 individual, for example: ,Fish_ID,Date,R2sqrt 1,1646,2006-08-18 08:48:59,0 2,1646,2006-08-18 09:53:20,100 I would like to read the data for all individuals in the for loop below and then combine them in a zoo object in order to plot them together. My question is : How do I need to set up the for loop to deal with several individuals ? I have put question marks the bits that I am missing. I very much appreciate your help. Regards, Juliane ReleaseDates - read.csv(file=ReleaseDates,head=TRUE,sep=,) #reads in the individuals and their release dates for (i in 1:length(ReleaseDates)){ Fish_ID - ReleaseDates$Fish_ID[i] ??? - read.zoo(file=paste(Results,Fish_ID,sep=_),index.column=3,header=TRUE,FUN=as.chron,sep=,) #reads in data for each in Fish_ID } z - na.approx(cbind(???), na.rm = FALSE) plot(z) Try this (untested): library(zoo) filenames - paste(Results, ReleaseDates$Fish_ID, sep = _) # list of zoo objects removing 1st two columns in each file Lz - lapply(filenames, read.zoo, header = TRUE, FUN = as.chron, sep = ,, colClasses = c(NULL, NULL, character, numeric)) z - do.call(merge, Lz) colnames(z) - as.character(ReleaseDates$Fish_ID) # plot in single panel plot(na.approx(z), screen = 1) -- Statistics Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com __ 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] plotting multiple animal tracks against Date/Time
Hi, I am sorry that my question wasn not very clearly formulated. My real data comes in 47 .csv files, one for each of 47 individual, for example: ,Fish_ID,Date,R2sqrt 1,1646,2006-08-18 08:48:59,0 2,1646,2006-08-18 09:53:20,100 I would like to read the data for all individuals in the for loop below and then combine them in a zoo object in order to plot them together. My question is : How do I need to set up the for loop to deal with several individuals ? I have put question marks the bits that I am missing. I very much appreciate your help. Regards, Juliane ReleaseDates - read.csv(file=ReleaseDates,head=TRUE,sep=,) #reads in the individuals and their release dates for (i in 1:length(ReleaseDates)){ Fish_ID - ReleaseDates$Fish_ID[i] ??? - read.zoo(file=paste(Results,Fish_ID,sep=_),index.column=3,header=TRUE,FUN=as.chron,sep=,) #reads in data for each in Fish_ID } z - na.approx(cbind(???), na.rm = FALSE) plot(z) From: Gabor Grothendieck [ggrothendi...@gmail.com] Sent: 24 September 2010 22:08 To: Struve, Juliane Cc: r-help@r-project.org Subject: Re: [R] plotting multiple animal tracks against Date/Time On Fri, Sep 24, 2010 at 4:16 PM, Struve, Juliane j.str...@imperial.ac.uk wrote: Hi again, when applying the code to my real data I need to deal with a large number of individuals and massive data sets. I am using the code below to read in the data for different individuals, and would like to create the Lines within the loop. But Lines needs to have the variable Fish_ID somehow included in the name, as otherwise the string will be overwritten on each execution. How can I create a different Lines for each Fish_ID ? Or is there a better way of doing this ? Sorry, this is surely a beginner's question. Thank you very much for your help. Regards, Juliane ReleaseDates - read.csv(file=ReleaseDates.csv,head=TRUE,sep=,) for (i in 1:length(ReleaseDates$Fish_ID)){ Fish_ID - ReleaseDates$Fish_ID[i] Data - read.csv(paste(Fish_ID)) Lines - paste(Data$Date,Data$Distance) print Lines } That was just an example. The purpose of Lines was to make the example self contained and reproducible. In reality you dont use Lines at all. Also your post suggested that they were in separate files since you had headings on each one. If that is not the case then you just read it all at once using read.zoo. z - read.zoo(ReleaseDates.csv, split = Fish_ID, header = TRUE, sep = ,, ...whatever...) There are three vignettes (pdf documents) that come with zoo. Read them all and read the help page of read.zoo. __ 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] plotting multiple animal tracks against Date/Time
On Mon, Sep 27, 2010 at 5:38 PM, Struve, Juliane j.str...@imperial.ac.uk wrote: Hello, thank you very much for replying. The code yields an error message Error in strptime(x, format, tz = tz) : invalid 'x' argument But I can't see what's wrong with it, the Date/Time info is in the third column, format is %Y-%m-%d %H:%M:%S. There is no time zone info in the data, could this be a problem ? See the last line on every message to r-help. You need to put together a minimal self contained example that illustrates the problem. The best I can do with the information provided is to point out that this works: Lines - ',Fish_ID,Date,R2sqrt + 1,1646,2006-08-18 08:48:59,0 + 2,1646,2006-08-18 09:53:20,100' library(zoo) library(chron) read.zoo(textConnection(Lines), header = TRUE, FUN = as.chron, + sep = ,, colClasses = c(NULL, NULL, character, numeric)) (08/18/06 08:48:59) (08/18/06 09:53:20) 0 100 -- Statistics Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com __ 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] plotting multiple animal tracks against Date/Time
Hello, thank you very much for replying. The code yields an error message Error in strptime(x, format, tz = tz) : invalid 'x' argument But I can't see what's wrong with it, the Date/Time info is in the third column, format is %Y-%m-%d %H:%M:%S. There is no time zone info in the data, could this be a problem ? Regards, Juliane Dr. Juliane Struve Daphne Jackson Fellow Imperial College London Department of Life Sciences Silwood Park Campus Buckhurst Road Ascot, Berkshire, SL5 7PY, UK Tel: +44 (0)20 7594 2527 Fax: +44 (0)1344 874 957 http://www.aquaticresources.org From: Gabor Grothendieck [ggrothendi...@gmail.com] Sent: 27 September 2010 15:23 To: Struve, Juliane Cc: r-help@r-project.org Subject: Re: [R] plotting multiple animal tracks against Date/Time On Mon, Sep 27, 2010 at 10:01 AM, Struve, Juliane j.str...@imperial.ac.uk wrote: Hi, I am sorry that my question wasn not very clearly formulated. My real data comes in 47 .csv files, one for each of 47 individual, for example: ,Fish_ID,Date,R2sqrt 1,1646,2006-08-18 08:48:59,0 2,1646,2006-08-18 09:53:20,100 I would like to read the data for all individuals in the for loop below and then combine them in a zoo object in order to plot them together. My question is : How do I need to set up the for loop to deal with several individuals ? I have put question marks the bits that I am missing. I very much appreciate your help. Regards, Juliane ReleaseDates - read.csv(file=ReleaseDates,head=TRUE,sep=,) #reads in the individuals and their release dates for (i in 1:length(ReleaseDates)){ Fish_ID - ReleaseDates$Fish_ID[i] ??? - read.zoo(file=paste(Results,Fish_ID,sep=_),index.column=3,header=TRUE,FUN=as.chron,sep=,) #reads in data for each in Fish_ID } z - na.approx(cbind(???), na.rm = FALSE) plot(z) Try this (untested): library(zoo) filenames - paste(Results, ReleaseDates$Fish_ID, sep = _) # list of zoo objects removing 1st two columns in each file Lz - lapply(filenames, read.zoo, header = TRUE, FUN = as.chron, sep = ,, colClasses = c(NULL, NULL, character, numeric)) z - do.call(merge, Lz) colnames(z) - as.character(ReleaseDates$Fish_ID) # plot in single panel plot(na.approx(z), screen = 1) -- Statistics Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com __ 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] plotting multiple animal tracks against Date/Time
Great, thanks ! It works beautifully now. Dr. Juliane Struve Daphne Jackson Fellow Imperial College London Department of Life Sciences Silwood Park Campus Buckhurst Road Ascot, Berkshire, SL5 7PY, UK Tel: +44 (0)20 7594 2527 Fax: +44 (0)1344 874 957 http://www.aquaticresources.orghttp://www.aquaticresources.org/ From: Gabor Grothendieck [ggrothendi...@gmail.com] Sent: 23 September 2010 23:06 To: Struve, Juliane Cc: r-help@r-project.org Subject: Re: [R] plotting multiple animal tracks against Date/Time On Thu, Sep 23, 2010 at 5:53 PM, Struve, Juliane j.str...@imperial.ac.ukmailto:j.str...@imperial.ac.uk wrote: Hello, thank you very much for replying. I have tried this, but I get error message Error in .subset(x, j) : invalid subscript type 'list' after z1 - read.zoo(textConnection(Lines1), skip = 1, index = list(1, 2), FUN = dt) Regards, Juliane It works for me. You likely have an outdated version of zoo. Make sure you have zoo 1.6-4. Here is the output: packageDescription(zoo)$Version [1] 1.6-4 Lines1 - DateDistance [m] + 2006-08-18 22:05:15 1815.798 + 2006-08-18 22:06:35 1815.798 + 2006-08-18 22:08:33 1815.798 + 2006-08-18 22:09:49 1815.798 + 2006-08-18 22:12:50 1815.798 + 2006-08-18 22:16:26 1815.798 Lines2 - Date Distance [m] + 2006-08-18 09:53:20 0.0 + 2006-08-18 09:59:07 0.0 + 2006-08-18 10:09:20 0.0 + 2006-08-18 10:21:14 0.0 + 2006-08-18 10:34:18 0.0 + 2006-08-18 10:36:44100.2 library(zoo) library(chron) # read in data dt - function(date, time) as.chron(paste(date, time)) z1 - read.zoo(textConnection(Lines1), skip = 1, index = list(1, 2), FUN = dt) z2 - read.zoo(textConnection(Lines2), skip = 1, index = list(1, 2), FUN = dt) # create single zoo object z - na.approx(cbind(z1, z2), na.rm = FALSE) Warning messages: 1: closing unused connection 4 (Lines2) 2: closing unused connection 3 (Lines1) # plot -- remove screen=1 if you want separate panels plot(z, screen = 1) -- Statistics Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.comhttp://gmail.com [[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.
Re: [R] plotting multiple animal tracks against Date/Time
Hello, thank you very much for replying. I have tried this, but I get error message Error in .subset(x, j) : invalid subscript type 'list' after z1 - read.zoo(textConnection(Lines1), skip = 1, index = list(1, 2), FUN = dt) Regards, Juliane Dr. Juliane Struve Daphne Jackson Fellow Imperial College London Department of Life Sciences Silwood Park Campus Buckhurst Road Ascot, Berkshire, SL5 7PY, UK Tel: +44 (0)20 7594 2527 Fax: +44 (0)1344 874 957 http://www.aquaticresources.org From: Gabor Grothendieck [ggrothendi...@gmail.com] Sent: 23 September 2010 18:26 To: Struve, Juliane Cc: r-help@r-project.org Subject: Re: [R] plotting multiple animal tracks against Date/Time On Thu, Sep 23, 2010 at 9:50 AM, Struve, Juliane j.str...@imperial.ac.uk wrote: Dear list, I would like to create a time series plot in which the paths of several individuals are stacked above each other, with the x-axis being the total observation period of three years ( 1.1.2004 to 31.12.2007) and the y-axis being some defined range[min,max]. My data consist of Date/Time information and the paths of 45 individual as the distance from the location of release. An example data set for 2 individuals is given below.The observation period and frequency of observations varies between individuals. I believe stackplot() may be able to do this task, but I am not sure how to handle the variable time period and frequency of observations for different individuals. Could someone advise if stackplot() is suitable or if there is a better approach or package ? Thank you very much for your time and best wishes, Juliane Try this: Lines1 - DateDistance [m] 2006-08-18 22:05:15 1815.798 2006-08-18 22:06:35 1815.798 2006-08-18 22:08:33 1815.798 2006-08-18 22:09:49 1815.798 2006-08-18 22:12:50 1815.798 2006-08-18 22:16:26 1815.798 Lines2 - Date Distance [m] 2006-08-18 09:53:20 0.0 2006-08-18 09:59:07 0.0 2006-08-18 10:09:20 0.0 2006-08-18 10:21:14 0.0 2006-08-18 10:34:18 0.0 2006-08-18 10:36:44100.2 library(chron) dt - function(date, time) as.chron(paste(date, time)) library(zoo) library(chron) # read in data dt - function(date, time) as.chron(paste(date, time)) z1 - read.zoo(textConnection(Lines1), skip = 1, index = list(1, 2), FUN = dt) z2 - read.zoo(textConnection(Lines2), skip = 1, index = list(1, 2), FUN = dt) # create single zoo object z - na.approx(cbind(z1, z2), na.rm = FALSE) # plot -- remove screen=1 if you want separate panels plot(z, screen = 1) -- Statistics Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com __ 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] plotting multiple animal tracks against Date/Time
On Fri, Sep 24, 2010 at 4:16 PM, Struve, Juliane j.str...@imperial.ac.ukwrote: Hi again, when applying the code to my real data I need to deal with a large number of individuals and massive data sets. I am using the code below to read in the data for different individuals, and would like to create the Lines within the loop. But Lines needs to have the variable Fish_ID somehow included in the name, as otherwise the string will be overwritten on each execution. How can I create a different Lines for each Fish_ID ? Or is there a better way of doing this ? Sorry, this is surely a beginner's question. Thank you very much for your help. Regards, Juliane ReleaseDates - read.csv(file=ReleaseDates.csv,head=TRUE,sep=,) for (i in 1:length(ReleaseDates$Fish_ID)){ Fish_ID - ReleaseDates$Fish_ID[i] Data - read.csv(paste(Fish_ID)) Lines - paste(Data$Date,Data$Distance) print Lines } That was just an example. The purpose of Lines was to make the example self contained and reproducible. In reality you dont use Lines at all. Also your post suggested that they were in separate files since you had headings on each one. If that is not the case then you just read it all at once using read.zoo. z - read.zoo(ReleaseDates.csv, split = Fish_ID, header = TRUE, sep = ,, ...whatever...) There are three vignettes (pdf documents) that come with zoo. Read them all and read the help page of read.zoo. [[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.
Re: [R] plotting multiple animal tracks against Date/Time
Hi again, when applying the code to my real data I need to deal with a large number of individuals and massive data sets. I am using the code below to read in the data for different individuals, and would like to create the Lines within the loop. But Lines needs to have the variable Fish_ID somehow included in the name, as otherwise the string will be overwritten on each execution. How can I create a different Lines for each Fish_ID ? Or is there a better way of doing this ? Sorry, this is surely a beginner's question. Thank you very much for your help. Regards, Juliane ReleaseDates - read.csv(file=ReleaseDates.csv,head=TRUE,sep=,) for (i in 1:length(ReleaseDates$Fish_ID)){ Fish_ID - ReleaseDates$Fish_ID[i] Data - read.csv(paste(Fish_ID)) Lines - paste(Data$Date,Data$Distance) print Lines } Dr. Juliane Struve Daphne Jackson Fellow Imperial College London Department of Life Sciences Silwood Park Campus Buckhurst Road Ascot, Berkshire, SL5 7PY, UK Tel: +44 (0)20 7594 2527 Fax: +44 (0)1344 874 957 http://www.aquaticresources.orghttp://www.aquaticresources.org/ From: Gabor Grothendieck [ggrothendi...@gmail.com] Sent: 23 September 2010 23:06 To: Struve, Juliane Cc: r-help@r-project.org Subject: Re: [R] plotting multiple animal tracks against Date/Time On Thu, Sep 23, 2010 at 5:53 PM, Struve, Juliane j.str...@imperial.ac.ukmailto:j.str...@imperial.ac.uk wrote: Hello, thank you very much for replying. I have tried this, but I get error message Error in .subset(x, j) : invalid subscript type 'list' after z1 - read.zoo(textConnection(Lines1), skip = 1, index = list(1, 2), FUN = dt) Regards, Juliane It works for me. You likely have an outdated version of zoo. Make sure you have zoo 1.6-4. Here is the output: packageDescription(zoo)$Version [1] 1.6-4 Lines1 - DateDistance [m] + 2006-08-18 22:05:15 1815.798 + 2006-08-18 22:06:35 1815.798 + 2006-08-18 22:08:33 1815.798 + 2006-08-18 22:09:49 1815.798 + 2006-08-18 22:12:50 1815.798 + 2006-08-18 22:16:26 1815.798 Lines2 - Date Distance [m] + 2006-08-18 09:53:20 0.0 + 2006-08-18 09:59:07 0.0 + 2006-08-18 10:09:20 0.0 + 2006-08-18 10:21:14 0.0 + 2006-08-18 10:34:18 0.0 + 2006-08-18 10:36:44100.2 library(zoo) library(chron) # read in data dt - function(date, time) as.chron(paste(date, time)) z1 - read.zoo(textConnection(Lines1), skip = 1, index = list(1, 2), FUN = dt) z2 - read.zoo(textConnection(Lines2), skip = 1, index = list(1, 2), FUN = dt) # create single zoo object z - na.approx(cbind(z1, z2), na.rm = FALSE) Warning messages: 1: closing unused connection 4 (Lines2) 2: closing unused connection 3 (Lines1) # plot -- remove screen=1 if you want separate panels plot(z, screen = 1) -- Statistics Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.comhttp://gmail.com [[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] plotting multiple animal tracks against Date/Time
Dear list, I would like to create a time series plot in which the paths of several individuals are stacked above each other, with the x-axis being the total observation period of three years ( 1.1.2004 to 31.12.2007) and the y-axis being some defined range[min,max]. My data consist of Date/Time information and the paths of 45 individual as the distance from the location of release. An example data set for 2 individuals is given below.The observation period and frequency of observations varies between individuals. I believe stackplot() may be able to do this task, but I am not sure how to handle the variable time period and frequency of observations for different individuals. Could someone advise if stackplot() is suitable or if there is a better approach or package ? Thank you very much for your time and best wishes, Juliane Individual 1 DateDistance [m] 2006-08-18 22:05:15 1815.798 2006-08-18 22:06:35 1815.798 2006-08-18 22:08:33 1815.798 2006-08-18 22:09:49 1815.798 2006-08-18 22:12:50 1815.798 2006-08-18 22:16:26 1815.798 Individual 2 Date Distance [m] 2006-08-18 09:53:20 0.0 2006-08-18 09:59:07 0.0 2006-08-18 10:09:20 0.0 2006-08-18 10:21:14 0.0 2006-08-18 10:34:18 0.0 2006-08-18 10:36:44100.2 2 Date Distance 6 2006-08-18 09:53:20 0.0 7 2006-08-18 09:59:07 0.0 8 2006-08-18 10:09:20 0.0 9 2006-08-18 10:21:14 0.0 10 2006-08-18 10:34:18 0.0 11 2006-08-18 10:36:44100.2 006-03-1 22:05:15 1815.798 2006-03-18 22:06:35 1815.798 2006-03-18 22:08:33 1815.798 2006-03-18 22:09:49 1815.798 2006-03-18 22:12:50 1815.798 2006-03-18 22:16:26 1815.798 Dr. Juliane Struve Imperial College London Department of Life Sciences Silwood Park Campus Buckhurst Road Ascot, Berkshire, SL5 7PY, UK Tel: +44 (0)20 7594 2527 Fax: +44 (0)1344 874 957 http://www.aquaticresources.org __ 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] Plotting multiple animal tracks against Date/Time
Sorry for posting this questions twice, but my previous question was accidentally sent unfinished. Dear list, I would like to create a time series plot in which the paths of several individuals are stacked above each other, with the x-axis being the total observation period of three years ( 1.1.2004 to 31.12.2007) and the y-axis being some defined range[min,max]. My data consist of Date/Time information and the paths of 45 individual as the distance from the location of release. An example data set for 2 individuals is given below.The observation period and frequency of observations varies between individuals. I believe stackplot() may be able to do this task, but I am not sure how to handle the variable time period and frequency of observations for different individuals. Could someone advise if stackplot() is suitable or if there is a better approach or package ? Thank you very much for your time, Juliane Individual 1 DateDistance [m] 2005-07-18 22:05:15 1815.798 2005-07-18 22:06:35 1815.798 2005-07-18 22:08:33 1815.798 2005-07-18 22:09:49 1815.798 2005-07-18 22:12:50 1815.798 2005-07-18 22:16:26 1815.798 Individual 2 Date Distance [m] 2006-08-18 09:53:20 0.0 2006-08-18 09:59:07 0.0 2006-08-18 10:09:20 0.0 2006-08-18 10:21:14 100.5 Dr. Juliane Struve Imperial College London Department of Life Sciences Ascot, Berkshire, SL5 7PY, UK __ 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] plotting multiple animal tracks against Date/Time
By range on the y-axis, do you mean distance? It would have to be if time is on your x? Or am I misreading this? You could just plot() with the data for your first individual, and then add additional individuals after that using lines(), specifying a different colour and/or line type for each individual, and could even plot points as well to identify your actual data collection points. Alternatively, you could do a bunch of multi-panel plots with the same axes, and individual data is given in each plot. It sounds like this is what you want to do by referencing stackplot() (based on it's description, I've never used it)? If so, see ?par for details, specifically mfrow, mfcol. By doing it with calls to par(), I think you'll have more control over the appearance of the plot than with stackplot(). Mike On Thu, Sep 23, 2010 at 8:50 AM, Struve, Juliane j.str...@imperial.ac.ukwrote: Dear list, I would like to create a time series plot in which the paths of several individuals are stacked above each other, with the x-axis being the total observation period of three years ( 1.1.2004 to 31.12.2007) and the y-axis being some defined range[min,max]. My data consist of Date/Time information and the paths of 45 individual as the distance from the location of release. An example data set for 2 individuals is given below.The observation period and frequency of observations varies between individuals. I believe stackplot() may be able to do this task, but I am not sure how to handle the variable time period and frequency of observations for different individuals. Could someone advise if stackplot() is suitable or if there is a better approach or package ? Thank you very much for your time and best wishes, Juliane Individual 1 DateDistance [m] 2006-08-18 22:05:15 1815.798 2006-08-18 22:06:35 1815.798 2006-08-18 22:08:33 1815.798 2006-08-18 22:09:49 1815.798 2006-08-18 22:12:50 1815.798 2006-08-18 22:16:26 1815.798 Individual 2 Date Distance [m] 2006-08-18 09:53:20 0.0 2006-08-18 09:59:07 0.0 2006-08-18 10:09:20 0.0 2006-08-18 10:21:14 0.0 2006-08-18 10:34:18 0.0 2006-08-18 10:36:44100.2 2 Date Distance 6 2006-08-18 09:53:20 0.0 7 2006-08-18 09:59:07 0.0 8 2006-08-18 10:09:20 0.0 9 2006-08-18 10:21:14 0.0 10 2006-08-18 10:34:18 0.0 11 2006-08-18 10:36:44100.2 006-03-1 22:05:15 1815.798 2006-03-18 22:06:35 1815.798 2006-03-18 22:08:33 1815.798 2006-03-18 22:09:49 1815.798 2006-03-18 22:12:50 1815.798 2006-03-18 22:16:26 1815.798 Dr. Juliane Struve Imperial College London Department of Life Sciences Silwood Park Campus Buckhurst Road Ascot, Berkshire, SL5 7PY, UK Tel: +44 (0)20 7594 2527 Fax: +44 (0)1344 874 957 http://www.aquaticresources.org __ 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.htmlhttp://www.r-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[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.
Re: [R] plotting multiple animal tracks against Date/Time
On Thu, Sep 23, 2010 at 9:50 AM, Struve, Juliane j.str...@imperial.ac.uk wrote: Dear list, I would like to create a time series plot in which the paths of several individuals are stacked above each other, with the x-axis being the total observation period of three years ( 1.1.2004 to 31.12.2007) and the y-axis being some defined range[min,max]. My data consist of Date/Time information and the paths of 45 individual as the distance from the location of release. An example data set for 2 individuals is given below.The observation period and frequency of observations varies between individuals. I believe stackplot() may be able to do this task, but I am not sure how to handle the variable time period and frequency of observations for different individuals. Could someone advise if stackplot() is suitable or if there is a better approach or package ? Thank you very much for your time and best wishes, Juliane Try this: Lines1 - DateDistance [m] 2006-08-18 22:05:15 1815.798 2006-08-18 22:06:35 1815.798 2006-08-18 22:08:33 1815.798 2006-08-18 22:09:49 1815.798 2006-08-18 22:12:50 1815.798 2006-08-18 22:16:26 1815.798 Lines2 - Date Distance [m] 2006-08-18 09:53:20 0.0 2006-08-18 09:59:07 0.0 2006-08-18 10:09:20 0.0 2006-08-18 10:21:14 0.0 2006-08-18 10:34:18 0.0 2006-08-18 10:36:44100.2 library(chron) dt - function(date, time) as.chron(paste(date, time)) library(zoo) library(chron) # read in data dt - function(date, time) as.chron(paste(date, time)) z1 - read.zoo(textConnection(Lines1), skip = 1, index = list(1, 2), FUN = dt) z2 - read.zoo(textConnection(Lines2), skip = 1, index = list(1, 2), FUN = dt) # create single zoo object z - na.approx(cbind(z1, z2), na.rm = FALSE) # plot -- remove screen=1 if you want separate panels plot(z, screen = 1) -- Statistics Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com __ 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] Plotting multiple animal tracks against Date/Time
Include individual as a factor in your dataset, and use ggplot2: library(ggplot2) ggplot(aes(x=Date, y=Distance, color=Individual), data=data) + geom_line() ought to do it. Jonathan On Thu, Sep 23, 2010 at 9:31 AM, Struve, Juliane j.str...@imperial.ac.ukwrote: Sorry for posting this questions twice, but my previous question was accidentally sent unfinished. Dear list, I would like to create a time series plot in which the paths of several individuals are stacked above each other, with the x-axis being the total observation period of three years ( 1.1.2004 to 31.12.2007) and the y-axis being some defined range[min,max]. My data consist of Date/Time information and the paths of 45 individual as the distance from the location of release. An example data set for 2 individuals is given below.The observation period and frequency of observations varies between individuals. I believe stackplot() may be able to do this task, but I am not sure how to handle the variable time period and frequency of observations for different individuals. Could someone advise if stackplot() is suitable or if there is a better approach or package ? Thank you very much for your time, Juliane Individual 1 DateDistance [m] 2005-07-18 22:05:15 1815.798 2005-07-18 22:06:35 1815.798 2005-07-18 22:08:33 1815.798 2005-07-18 22:09:49 1815.798 2005-07-18 22:12:50 1815.798 2005-07-18 22:16:26 1815.798 Individual 2 Date Distance [m] 2006-08-18 09:53:20 0.0 2006-08-18 09:59:07 0.0 2006-08-18 10:09:20 0.0 2006-08-18 10:21:14 100.5 Dr. Juliane Struve Imperial College London Department of Life Sciences Ascot, Berkshire, SL5 7PY, UK __ 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. [[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.
Re: [R] plotting multiple animal tracks against Date/Time
On Thu, Sep 23, 2010 at 5:53 PM, Struve, Juliane j.str...@imperial.ac.ukwrote: Hello, thank you very much for replying. I have tried this, but I get error message Error in .subset(x, j) : invalid subscript type 'list' after z1 - read.zoo(textConnection(Lines1), skip = 1, index = list(1, 2), FUN = dt) Regards, Juliane It works for me. You likely have an outdated version of zoo. Make sure you have zoo 1.6-4. Here is the output: packageDescription(zoo)$Version [1] 1.6-4 Lines1 - DateDistance [m] + 2006-08-18 22:05:15 1815.798 + 2006-08-18 22:06:35 1815.798 + 2006-08-18 22:08:33 1815.798 + 2006-08-18 22:09:49 1815.798 + 2006-08-18 22:12:50 1815.798 + 2006-08-18 22:16:26 1815.798 Lines2 - Date Distance [m] + 2006-08-18 09:53:20 0.0 + 2006-08-18 09:59:07 0.0 + 2006-08-18 10:09:20 0.0 + 2006-08-18 10:21:14 0.0 + 2006-08-18 10:34:18 0.0 + 2006-08-18 10:36:44100.2 library(zoo) library(chron) # read in data dt - function(date, time) as.chron(paste(date, time)) z1 - read.zoo(textConnection(Lines1), skip = 1, index = list(1, 2), FUN = dt) z2 - read.zoo(textConnection(Lines2), skip = 1, index = list(1, 2), FUN = dt) # create single zoo object z - na.approx(cbind(z1, z2), na.rm = FALSE) Warning messages: 1: closing unused connection 4 (Lines2) 2: closing unused connection 3 (Lines1) # plot -- remove screen=1 if you want separate panels plot(z, screen = 1) -- Statistics Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com [[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.