Re: [R] Inserting rows of interpolated data

2013-02-12 Thread PIKAL Petr
Hi

Why you do not change date and time to POSIX object? It is simple and saves you 
a lot of frustration when merging two data frames.

If you changed lightdata date and time to new column

lightdata$newdate - strptime(paste(lightdata$date, lightdata$time, sep= ), 
format = %d/%m/%y %H:%M:%S)

generate empty
empty -data.frame(newdate= seq(firstdate, lastdate, by=min), light=NA)
see ?seq.POSIXt for details

new - merge(lightdata, empty, by=newdate, all=TRUE)

shall result in merged dataframes

Regards
Petr

 -Original Message-
 From: r-help-boun...@r-project.org [mailto:r-help-bounces@r-
 project.org] On Behalf Of Benstead, Jonathan
 Sent: Tuesday, February 12, 2013 12:19 AM
 To: r-help@r-project.org
 Subject: [R] Inserting rows of interpolated data
 
 Dear help list - I have light data with 5-min time-stamps. I would like
 to insert four 1-min time-stamps between each row and interpolate the
 light data on each new row. To do this I have come up with the
 following code:
 
 lightdata - read.table(Test_light_data.csv, header = TRUE, sep =
 ,)  # read data file into object lightdata
 library(chron)
 mins - data.frame(times(1:1439/1440)) # generate a dataframe of 24
 hours of 1-min timestamps Nth.delete - function(dataframe,
 n)dataframe[-(seq(n, to=nrow(dataframe), by=n)),] # function for
 deleting nth row empty - data.frame(1/9/13, Nth.delete(mins, 5),
 NA) # delete all 5-min timestamps in a new dataframe
 colnames(empty) - c(date, time, light) # add correct column name
 to empty timestamp dataframe newdata - rbind(lightdata, empty)
 
 I get the following error message:
 
 Warning message:
 In `[-.factor`(`*tmp*`, ri, value = c(0.000694,
 0.00139,  :
   invalid factor level, NAs generated
 
 Digging into this a little, I can see that the two time columns are
 doing what I need and APPEAR to be similar in format:
 
  head(lightdata)
 datetime   light
 1 1/9/13 0:00:00 -0.00040925
 2 1/9/13 0:05:00 -0.00023386
 3 1/9/13 0:10:00 -0.00032155
 4 1/9/13 0:15:00 -0.00017539
 5 1/9/13 0:20:00 -0.00029232
 6 1/9/13 0:25:00 -0.00038002
 
  head(empty)
 date time light
 1 1/9/13 00:01:00NA
 2 1/9/13 00:02:00NA
 3 1/9/13 00:03:00NA
 4 1/9/13 00:04:00NA
 5 1/9/13 00:06:00NA
 6 1/9/13 00:07:00NA
 
 but they clearly are not as far as R is concerned, as shown by str:
 
  str(lightdata)
 'data.frame':   288 obs. of  3 variables:
  $ date : Factor w/ 1 level 1/9/13: 1 1 1 1 1 1 1 1 1 1 ...
  $ time : Factor w/ 288 levels 0:00:00,0:05:00,..: 1 2 3 4 5 6 7 8
 9 10 ...
  $ light: num  -0.000409 -0.000234 -0.000322 -0.000175 -0.000292 ...
 
  str(empty)
 'data.frame':   1152 obs. of  3 variables:
  $ date : Factor w/ 1 level 1/9/13: 1 1 1 1 1 1 1 1 1 1 ...
  $ time :Class 'times'  atomic [1:1152] 0.000694 0.001389 0.002083
 0.002778 0.004167 ...
   .. ..- attr(*, format)= chr h:m:s
  $ light: Factor w/ 1 level NA: 1 1 1 1 1 1 1 1 1 1 ...
 
 In the first (original) dataframe, light is a factor, while in the
 dataframe of generated timestamps, the timestamps are actually still in
 fractions of a day.
 
 Presumably this is why rbind is not working? Can anyone help? By the
 way, I know I can use na.approx in zoo to do the eventual interpolation
 of the light data. It's getting there that has me stumped for now.
 
 Many thanks, Jon (new R user).
 __
 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] Inserting rows of interpolated data

2013-02-12 Thread Joshua Ulrich
Hi Jon,

zoo is great for tasks like this, not just for na.approx. :)

I would approach the problem like this:

library(zoo)
# put lightdata into a zoo object
z - with(lightdata, zoo(light,
  as.POSIXct(paste(date, time), format=%m/%d/%y %H:%M:%S)))
# merge the above zoo object with an empty zoo
# object that has all the index values you want
Z - merge(z, zoo(,seq(start(z),end(z),by=1 min)))
# interpolate between the 5-min observatoins
Z - na.approx(Z)

HTH,
--
Joshua Ulrich  |  about.me/joshuaulrich
FOSS Trading  |  www.fosstrading.com

R/Finance 2013: Applied Finance with R  | www.RinFinance.com


On Mon, Feb 11, 2013 at 5:19 PM, Benstead, Jonathan jbenst...@as.ua.edu wrote:
 Dear help list - I have light data with 5-min time-stamps. I would like to 
 insert four 1-min time-stamps between each row and interpolate the light data 
 on each new row. To do this I have come up with the following code:

 lightdata - read.table(Test_light_data.csv, header = TRUE, sep = ,)  # 
 read data file into object lightdata
 library(chron)
 mins - data.frame(times(1:1439/1440)) # generate a dataframe of 24 hours of 
 1-min timestamps
 Nth.delete - function(dataframe, n)dataframe[-(seq(n, to=nrow(dataframe), 
 by=n)),] # function for deleting nth row
 empty - data.frame(1/9/13, Nth.delete(mins, 5), NA) # delete all 5-min 
 timestamps in a new dataframe
 colnames(empty) - c(date, time, light) # add correct column name to 
 empty timestamp dataframe
 newdata - rbind(lightdata, empty)

 I get the following error message:

 Warning message:
 In `[-.factor`(`*tmp*`, ri, value = c(0.000694, 
 0.00139,  :
   invalid factor level, NAs generated

 Digging into this a little, I can see that the two time columns are doing 
 what I need and APPEAR to be similar in format:

 head(lightdata)
 datetime   light
 1 1/9/13 0:00:00 -0.00040925
 2 1/9/13 0:05:00 -0.00023386
 3 1/9/13 0:10:00 -0.00032155
 4 1/9/13 0:15:00 -0.00017539
 5 1/9/13 0:20:00 -0.00029232
 6 1/9/13 0:25:00 -0.00038002

 head(empty)
 date time light
 1 1/9/13 00:01:00NA
 2 1/9/13 00:02:00NA
 3 1/9/13 00:03:00NA
 4 1/9/13 00:04:00NA
 5 1/9/13 00:06:00NA
 6 1/9/13 00:07:00NA

 but they clearly are not as far as R is concerned, as shown by str:

 str(lightdata)
 'data.frame':   288 obs. of  3 variables:
  $ date : Factor w/ 1 level 1/9/13: 1 1 1 1 1 1 1 1 1 1 ...
  $ time : Factor w/ 288 levels 0:00:00,0:05:00,..: 1 2 3 4 5 6 7 8 9 10 
 ...
  $ light: num  -0.000409 -0.000234 -0.000322 -0.000175 -0.000292 ...

 str(empty)
 'data.frame':   1152 obs. of  3 variables:
  $ date : Factor w/ 1 level 1/9/13: 1 1 1 1 1 1 1 1 1 1 ...
  $ time :Class 'times'  atomic [1:1152] 0.000694 0.001389 0.002083 0.002778 
 0.004167 ...
   .. ..- attr(*, format)= chr h:m:s
  $ light: Factor w/ 1 level NA: 1 1 1 1 1 1 1 1 1 1 ...

 In the first (original) dataframe, light is a factor, while in the dataframe 
 of generated timestamps, the timestamps are actually still in fractions of a 
 day.

 Presumably this is why rbind is not working? Can anyone help? By the way, I 
 know I can use na.approx in zoo to do the eventual interpolation of the light 
 data. It's getting there that has me stumped for now.

 Many thanks, Jon (new R user).
 __
 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] Inserting rows

2009-10-23 Thread Henrique Dallazuanna
Try this:

rbind(df, matrix(0, 3, 3, dimnames = list(NULL, names(df


On Fri, Oct 23, 2009 at 11:44 AM, Ashta sewa...@gmail.com wrote:
 Hi all,

 I have the data set  df with three varaibles,

 x1 x2 x3
 1  2   5
 2  4   1
 5  6   0
 1  1   2

 I want to insert more rows ( eg, 3 rows with value  filled with zeros)
 1  2   5
 2  4   1
 5  6   6
 1  1   2
 0  0  0
 0  0  0
 0  0  0

 Can any body help me out?

 Thanks

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




-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40 S 49° 16' 22 O

__
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] Inserting rows

2009-10-23 Thread John Kane
?rbind

df1 - data.frame(matrix(rep(0,9),nrow=3))
names(df1) - c(x1,x2,x3)

rbind(df,df1)

--- On Fri, 10/23/09, Ashta sewa...@gmail.com wrote:

 From: Ashta sewa...@gmail.com
 Subject: [R] Inserting rows
 To: R help r-help@r-project.org
 Received: Friday, October 23, 2009, 9:44 AM
 Hi all,
 
 I have the data set  df with three varaibles,
 
 x1 x2 x3
 1  2   5
 2  4   1
 5  6   0
 1  1   2
 
 I want to insert more rows ( eg, 3 rows with value 
 filled with zeros)
 1  2   5
 2  4   1
 5  6   6
 1  1   2
 0  0  0
 0  0  0
 0  0  0
 
 Can any body help me out?
 
 Thanks
 
 __
 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.
 


  __
Looking for the perfect gift? Give the gift of Flickr! 

http://www.flickr.com/gift/

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