An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20120924/e17bfc56/attachment.pl>
POSIXct coerced into numeric when filling a data frame
6 messages · Arnaud Duranel, jim holtman, arun
HI,
Try this:
dat1<-do.call(data.frame,x)
dat1<-data.frame(ID=letters[1:4],dat1)
dat1
#? ID?????????????? first????????????? second
#1? a 2011-08-27 10:45:00 2011-08-27 11:00:00
#2? b 2011-10-30 15:45:00 2011-10-30 15:30:00
#3? c 2011-10-30 16:00:00 2011-10-30 15:45:00
#4? d 2012-06-22 09:30:00 2012-06-22 10:00:00
?str(dat1)
#'data.frame':??? 4 obs. of? 3 variables:
# $ ID??? : Factor w/ 4 levels "a","b","c","d": 1 2 3 4
?#$ first : POSIXct, format: "2011-08-27 10:45:00" "2011-10-30 15:45:00" ...
?#$ second: POSIXct, format: "2011-08-27 11:00:00" "2011-10-30 15:30:00" ...
dat2<-dat1
?dat2$first<-as.Date(dat2$first,format="%Y-%m-%d %HH:MM:%SS")
?dat2$second<-as.Date(dat2$second,format="%Y-%m-%d %HH:MM:%SS")
?dat2
#? ID????? first???? second
#1? a 2011-08-27 2011-08-27
#2? b 2011-10-30 2011-10-30
#3? c 2011-10-30 2011-10-30
#4? d 2012-06-22 2012-06-22
?str(dat2)
#'data.frame':??? 4 obs. of? 3 variables:
# $ ID??? : Factor w/ 4 levels "a","b","c","d": 1 2 3 4
# $ first : Date, format: "2011-08-27" "2011-10-30" ...
# $ second: Date, format: "2011-08-27" "2011-10-30" ...
dat2<-within(dat2,ID<-as.character(ID))
subset(dat2,ID%in% c("a","b"))
#? ID????? first???? second
#1? a 2011-08-27 2011-08-27
#2? b 2011-10-30 2011-10-30
A.K.
----- Original Message -----
From: Arnaud Duranel <arnaud.duranel.09 at ucl.ac.uk>
To: r-help at r-project.org
Cc:
Sent: Monday, September 24, 2012 4:06 AM
Subject: [R] POSIXct coerced into numeric when filling a data frame
Hello
I have a list of POSIXct objects, from which I want to extract those
values that match a specific date, keeping them in POSIXct format. For a
specific date there is 0-2 matching values.
As an example (the actual list and objects are much longer):
x<-list()
x[["first"]]<-as.POSIXct(c("2011-08-27 10:45:00 GMT", "2011-10-30
15:45:00 GMT", "2011-10-30 16:00:00 GMT", "2012-06-22 09:30:00 GMT"))
x[["second"]]<-as.POSIXct(c("2011-08-27 11:00:00 GMT", "2011-10-30
15:30:00 GMT", "2011-10-30 15:45:00 GMT", "2012-06-22 10:00:00 GMT"))
If I use the following expression for one specific object of the list, I
get the result I expect:
x[[1]][as.Date(x[[1]])=="2011/10/30"][1]
[1] "2011-10-30 15:45:00 GMT"
Now if I write a for loop based on that expression to store the values
of interest in a data frame, the POSIXct values are coerced into numeric:
y<-data.frame()
for (i in 1:length(x)) {
? y[i,"ID"]<-names(x[i])
? y[i,"first"]<-x[[i]][as.Date(x[[i]])=="2011/10/30"][1]
? y[i,"second"]<-x[[i]][as.Date(x[[i]])=="2011/10/30"][2]
}
? ID? ? ? first? ? second
1? a 1319989500 1319990400
2? b 1319988600 1319989500
I am a bit confused about why that is.
I could coerce them back to POSIXct with another line of code, but is
there a better way? And is there a way to avoid using a for loop to
complete this task?
No need to say I am quite new to R, so apologies for any obvious mistake
or oversight.
Many thanks
A. Duranel, UCL Department of Geography
??? [[alternative HTML version deleted]]
______________________________________________
R-help at 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.
A.K., many thanks for your reply.
I have realised there was a mistake in my code, and that the example
could have been clearer; my apologies for this, the corrected code is below:
> x<-list()
> x[["a"]]<-as.POSIXct(c("2011-08-27 10:45:00 GMT", "2011-10-30
15:45:00 GMT", "2011-10-30 16:00:00 GMT", "2012-06-22 09:30:00 GMT",
"2012-06-22 10:00:00 GMT"))
> x[["b"]]<-as.POSIXct(c("2011-08-27 11:00:00 GMT", "2011-10-30
15:30:00 GMT", "2011-10-30 15:45:00 GMT", "2012-06-22 10:00:00 GMT"))
>
>
> x[[1]][as.Date(x[[1]])=="2011/10/30"][1]
[1] "2011-10-30 15:45:00 GMT"
>
> y<-data.frame()
> for (i in 1:length(x)) {
+ y[i,"ID"]<-names(x[i])
+ y[i,"first"]<-x[[i]][as.Date(x[[i]])=="2011/10/30"][1]
+ y[i,"second"]<-x[[i]][as.Date(x[[i]])=="2011/10/30"][2]
+ }
> y
ID first second
1 a 1319989500 1319990400
2 b 1319988600 1319989500
where I would need:
> y
ID first
second
1 a 2011-10-30 15:45:00 2011-10-30 16:00:00
2 b 2011-10-30 15:30:00 2011-10-30 15:45:00
The POSIXct objects in my list do not have the same length; and I need
to extract those POSIXct values that match a specific date (2011-10-30
in this example), while keeping them in POSIXct format.
Many thanks
Arnaud
On 24/09/2012 13:29, arun wrote:
HI,
Try this:
dat1<-do.call(data.frame,x)
dat1<-data.frame(ID=letters[1:4],dat1)
dat1
# ID first second
#1 a 2011-08-27 10:45:00 2011-08-27 11:00:00
#2 b 2011-10-30 15:45:00 2011-10-30 15:30:00
#3 c 2011-10-30 16:00:00 2011-10-30 15:45:00
#4 d 2012-06-22 09:30:00 2012-06-22 10:00:00
str(dat1)
#'data.frame': 4 obs. of 3 variables:
# $ ID : Factor w/ 4 levels "a","b","c","d": 1 2 3 4
#$ first : POSIXct, format: "2011-08-27 10:45:00" "2011-10-30 15:45:00" ...
#$ second: POSIXct, format: "2011-08-27 11:00:00" "2011-10-30 15:30:00" ...
dat2<-dat1
dat2$first<-as.Date(dat2$first,format="%Y-%m-%d %HH:MM:%SS")
dat2$second<-as.Date(dat2$second,format="%Y-%m-%d %HH:MM:%SS")
dat2
# ID first second
#1 a 2011-08-27 2011-08-27
#2 b 2011-10-30 2011-10-30
#3 c 2011-10-30 2011-10-30
#4 d 2012-06-22 2012-06-22
str(dat2)
#'data.frame': 4 obs. of 3 variables:
# $ ID : Factor w/ 4 levels "a","b","c","d": 1 2 3 4
# $ first : Date, format: "2011-08-27" "2011-10-30" ...
# $ second: Date, format: "2011-08-27" "2011-10-30" ...
dat2<-within(dat2,ID<-as.character(ID))
subset(dat2,ID%in% c("a","b"))
# ID first second
#1 a 2011-08-27 2011-08-27
#2 b 2011-10-30 2011-10-30
A.K.
----- Original Message -----
From: Arnaud Duranel <arnaud.duranel.09 at ucl.ac.uk>
To: r-help at r-project.org
Cc:
Sent: Monday, September 24, 2012 4:06 AM
Subject: [R] POSIXct coerced into numeric when filling a data frame
Hello
I have a list of POSIXct objects, from which I want to extract those
values that match a specific date, keeping them in POSIXct format. For a
specific date there is 0-2 matching values.
As an example (the actual list and objects are much longer):
x<-list()
x[["first"]]<-as.POSIXct(c("2011-08-27 10:45:00 GMT", "2011-10-30
15:45:00 GMT", "2011-10-30 16:00:00 GMT", "2012-06-22 09:30:00 GMT"))
x[["second"]]<-as.POSIXct(c("2011-08-27 11:00:00 GMT", "2011-10-30
15:30:00 GMT", "2011-10-30 15:45:00 GMT", "2012-06-22 10:00:00 GMT"))
If I use the following expression for one specific object of the list, I
get the result I expect:
x[[1]][as.Date(x[[1]])=="2011/10/30"][1]
[1] "2011-10-30 15:45:00 GMT"
Now if I write a for loop based on that expression to store the values
of interest in a data frame, the POSIXct values are coerced into numeric:
y<-data.frame()
for (i in 1:length(x)) {
y[i,"ID"]<-names(x[i])
y[i,"first"]<-x[[i]][as.Date(x[[i]])=="2011/10/30"][1]
y[i,"second"]<-x[[i]][as.Date(x[[i]])=="2011/10/30"][2]
}
ID first second
1 a 1319989500 1319990400
2 b 1319988600 1319989500
I am a bit confused about why that is.
I could coerce them back to POSIXct with another line of code, but is
there a better way? And is there a way to avoid using a for loop to
complete this task?
No need to say I am quite new to R, so apologies for any obvious mistake
or oversight.
Many thanks
A. Duranel, UCL Department of Geography
[[alternative HTML version deleted]]
______________________________________________ R-help at 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. .
Hi,
Upon running your code, the "y" I am getting is:
y
#? ID????? first???? second
#1? a 1320003900 1320004800
#2? b 1320003000 1320003900
This you can convert back by:
within(y,{first<-as.POSIXct(first,origin="1970-01-01");second<-as.POSIXct(second,origin="1970-01-01")})
#? ID?????????????? first????????????? second
#1? a 2011-10-30 20:45:00 2011-10-30 21:00:00
#2? b 2011-10-30 20:30:00 2011-10-30 20:45:00
A.K.
----- Original Message -----
From: Arnaud Duranel <arnaud.duranel.09 at ucl.ac.uk>
To: arun <smartpink111 at yahoo.com>
Cc: R help <r-help at r-project.org>
Sent: Monday, September 24, 2012 10:30 AM
Subject: [R] POSIXct coerced into numeric when filling a data frame
A.K., many thanks for your reply.
I have realised there was a mistake in my code, and that the example
could have been clearer; my apologies for this, the corrected code is below:
x<-list()
x[["a"]]<-as.POSIXct(c("2011-08-27 10:45:00 GMT", "2011-10-30
15:45:00 GMT", "2011-10-30 16:00:00 GMT", "2012-06-22 09:30:00 GMT", "2012-06-22 10:00:00 GMT"))
x[["b"]]<-as.POSIXct(c("2011-08-27 11:00:00 GMT", "2011-10-30
15:30:00 GMT", "2011-10-30 15:45:00 GMT", "2012-06-22 10:00:00 GMT"))
x[[1]][as.Date(x[[1]])=="2011/10/30"][1]
[1] "2011-10-30 15:45:00 GMT"
y<-data.frame()
for (i in 1:length(x)) {
+? y[i,"ID"]<-names(x[i]) +? y[i,"first"]<-x[[i]][as.Date(x[[i]])=="2011/10/30"][1] +? y[i,"second"]<-x[[i]][as.Date(x[[i]])=="2011/10/30"][2] + }
y
? ID? ? ? first? ? second 1? a 1319989500 1319990400 2? b 1319988600 1319989500 where I would need:
y
? ? ? ? ? ? ? ? ID? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? first? ? ? ? ? ? ? ? ? ? ? second 1? ? ? ? ? ? ? a? ? 2011-10-30 15:45:00? ? 2011-10-30 16:00:00 2? ? ? ? ? ? ? b? ? 2011-10-30 15:30:00? ? 2011-10-30 15:45:00 The POSIXct objects in my list do not have the same length; and I need to extract those POSIXct values that match a specific date (2011-10-30 in this example), while keeping them in POSIXct format. Many thanks Arnaud
On 24/09/2012 13:29, arun wrote:
HI,
Try this:
dat1<-do.call(data.frame,x)
dat1<-data.frame(ID=letters[1:4],dat1)
dat1
#? ID? ? ? ? ? ? ? first? ? ? ? ? ? ? second
#1? a 2011-08-27 10:45:00 2011-08-27 11:00:00
#2? b 2011-10-30 15:45:00 2011-10-30 15:30:00
#3? c 2011-10-30 16:00:00 2011-10-30 15:45:00
#4? d 2012-06-22 09:30:00 2012-06-22 10:00:00
? str(dat1)
#'data.frame':? ? 4 obs. of? 3 variables:
# $ ID? ? : Factor w/ 4 levels "a","b","c","d": 1 2 3 4
? #$ first : POSIXct, format: "2011-08-27 10:45:00" "2011-10-30 15:45:00" ...
? #$ second: POSIXct, format: "2011-08-27 11:00:00" "2011-10-30 15:30:00" ...
dat2<-dat1
? dat2$first<-as.Date(dat2$first,format="%Y-%m-%d %HH:MM:%SS")
? dat2$second<-as.Date(dat2$second,format="%Y-%m-%d %HH:MM:%SS")
? dat2
#? ID? ? ? first? ? second
#1? a 2011-08-27 2011-08-27
#2? b 2011-10-30 2011-10-30
#3? c 2011-10-30 2011-10-30
#4? d 2012-06-22 2012-06-22
? str(dat2)
#'data.frame':? ? 4 obs. of? 3 variables:
# $ ID? ? : Factor w/ 4 levels "a","b","c","d": 1 2 3 4
# $ first : Date, format: "2011-08-27" "2011-10-30" ...
# $ second: Date, format: "2011-08-27" "2011-10-30" ...
dat2<-within(dat2,ID<-as.character(ID))
subset(dat2,ID%in% c("a","b"))
#? ID? ? ? first? ? second
#1? a 2011-08-27 2011-08-27
#2? b 2011-10-30 2011-10-30
A.K.
----- Original Message -----
From: Arnaud Duranel <arnaud.duranel.09 at ucl.ac.uk>
To: r-help at r-project.org
Cc:
Sent: Monday, September 24, 2012 4:06 AM
Subject: [R] POSIXct coerced into numeric when filling a data frame
Hello
I have a list of POSIXct objects, from which I want to extract those
values that match a specific date, keeping them in POSIXct format. For a
specific date there is 0-2 matching values.
As an example (the actual list and objects are much longer):
x<-list()
x[["first"]]<-as.POSIXct(c("2011-08-27 10:45:00 GMT", "2011-10-30
15:45:00 GMT", "2011-10-30 16:00:00 GMT", "2012-06-22 09:30:00 GMT"))
x[["second"]]<-as.POSIXct(c("2011-08-27 11:00:00 GMT", "2011-10-30
15:30:00 GMT", "2011-10-30 15:45:00 GMT", "2012-06-22 10:00:00 GMT"))
If I use the following expression for one specific object of the list, I
get the result I expect:
x[[1]][as.Date(x[[1]])=="2011/10/30"][1]
[1] "2011-10-30 15:45:00 GMT"
Now if I write a for loop based on that expression to store the values
of interest in a data frame, the POSIXct values are coerced into numeric:
y<-data.frame()
for (i in 1:length(x)) {
? ? y[i,"ID"]<-names(x[i])
? ? y[i,"first"]<-x[[i]][as.Date(x[[i]])=="2011/10/30"][1]
? ? y[i,"second"]<-x[[i]][as.Date(x[[i]])=="2011/10/30"][2]
}
? ? ID? ? ? first? ? second
1? a 1319989500 1319990400
2? b 1319988600 1319989500
I am a bit confused about why that is.
I could coerce them back to POSIXct with another line of code, but is
there a better way? And is there a way to avoid using a for loop to
complete this task?
No need to say I am quite new to R, so apologies for any obvious mistake
or oversight.
Many thanks
A. Duranel, UCL Department of Geography
? ? ? [[alternative HTML version deleted]]
______________________________________________ R-help at 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. .
Try this; does away with the 'for', but have to convert back to POSIXct since sapply strips off the class:
x<-list()
x[["a"]]<-as.POSIXct(c("2011-08-27 10:45:00 GMT", "2011-10-30 15:45:00 GMT", "2011-10-30 16:00:00 GMT", "2012-06-22 09:30:00 GMT", "2012-06-22 10:00:00 GMT"))
x[["b"]]<-as.POSIXct(c("2011-08-27 11:00:00 GMT", "2011-10-30 15:30:00 GMT", "2011-10-30 15:45:00 GMT", "2012-06-22 10:00:00 GMT"))
x[[1]][as.Date(x[[1]])=="2011/10/30"][1]
[1] "2011-10-30 15:45:00 EDT"
# convert to POSIXct
unix2POSIXct <- function (time) structure(time, class = c("POSIXct", "POSIXt"))
y <- data.frame(ID = names(x)
+ , first = unix2POSIXct(sapply(x, function(d) d[as.Date(d) == "2011-10-30"][1])) + , second = unix2POSIXct(sapply(x, function(d) d[as.Date(d) == "2011-10-30"][2])) + )
y
ID first second a a 2011-10-30 15:45:00 2011-10-30 16:00:00 b b 2011-10-30 15:30:00 2011-10-30 15:45:00 On Mon, Sep 24, 2012 at 10:30 AM, Arnaud Duranel
<arnaud.duranel.09 at ucl.ac.uk> wrote:
A.K., many thanks for your reply. I have realised there was a mistake in my code, and that the example could have been clearer; my apologies for this, the corrected code is below:
x<-list()
x[["a"]]<-as.POSIXct(c("2011-08-27 10:45:00 GMT", "2011-10-30 15:45:00
GMT", "2011-10-30 16:00:00 GMT", "2012-06-22 09:30:00 GMT", "2012-06-22
10:00:00 GMT"))
x[["b"]]<-as.POSIXct(c("2011-08-27 11:00:00 GMT", "2011-10-30 15:30:00
GMT", "2011-10-30 15:45:00 GMT", "2012-06-22 10:00:00 GMT"))
x[[1]][as.Date(x[[1]])=="2011/10/30"][1]
[1] "2011-10-30 15:45:00 GMT"
y<-data.frame()
for (i in 1:length(x)) {
+ y[i,"ID"]<-names(x[i]) + y[i,"first"]<-x[[i]][as.Date(x[[i]])=="2011/10/30"][1] + y[i,"second"]<-x[[i]][as.Date(x[[i]])=="2011/10/30"][2] + }
y
ID first second 1 a 1319989500 1319990400 2 b 1319988600 1319989500 where I would need:
y
ID first second 1 a 2011-10-30 15:45:00 2011-10-30 16:00:00 2 b 2011-10-30 15:30:00 2011-10-30 15:45:00 The POSIXct objects in my list do not have the same length; and I need to extract those POSIXct values that match a specific date (2011-10-30 in this example), while keeping them in POSIXct format. Many thanks Arnaud On 24/09/2012 13:29, arun wrote:
HI,
Try this:
dat1<-do.call(data.frame,x)
dat1<-data.frame(ID=letters[1:4],dat1)
dat1
# ID first second
#1 a 2011-08-27 10:45:00 2011-08-27 11:00:00
#2 b 2011-10-30 15:45:00 2011-10-30 15:30:00
#3 c 2011-10-30 16:00:00 2011-10-30 15:45:00
#4 d 2012-06-22 09:30:00 2012-06-22 10:00:00
str(dat1)
#'data.frame': 4 obs. of 3 variables:
# $ ID : Factor w/ 4 levels "a","b","c","d": 1 2 3 4
#$ first : POSIXct, format: "2011-08-27 10:45:00" "2011-10-30 15:45:00"
...
#$ second: POSIXct, format: "2011-08-27 11:00:00" "2011-10-30 15:30:00"
...
dat2<-dat1
dat2$first<-as.Date(dat2$first,format="%Y-%m-%d %HH:MM:%SS")
dat2$second<-as.Date(dat2$second,format="%Y-%m-%d %HH:MM:%SS")
dat2
# ID first second
#1 a 2011-08-27 2011-08-27
#2 b 2011-10-30 2011-10-30
#3 c 2011-10-30 2011-10-30
#4 d 2012-06-22 2012-06-22
str(dat2)
#'data.frame': 4 obs. of 3 variables:
# $ ID : Factor w/ 4 levels "a","b","c","d": 1 2 3 4
# $ first : Date, format: "2011-08-27" "2011-10-30" ...
# $ second: Date, format: "2011-08-27" "2011-10-30" ...
dat2<-within(dat2,ID<-as.character(ID))
subset(dat2,ID%in% c("a","b"))
# ID first second
#1 a 2011-08-27 2011-08-27
#2 b 2011-10-30 2011-10-30
A.K.
----- Original Message -----
From: Arnaud Duranel <arnaud.duranel.09 at ucl.ac.uk>
To: r-help at r-project.org
Cc:
Sent: Monday, September 24, 2012 4:06 AM
Subject: [R] POSIXct coerced into numeric when filling a data frame
Hello
I have a list of POSIXct objects, from which I want to extract those
values that match a specific date, keeping them in POSIXct format. For a
specific date there is 0-2 matching values.
As an example (the actual list and objects are much longer):
x<-list()
x[["first"]]<-as.POSIXct(c("2011-08-27 10:45:00 GMT", "2011-10-30
15:45:00 GMT", "2011-10-30 16:00:00 GMT", "2012-06-22 09:30:00 GMT"))
x[["second"]]<-as.POSIXct(c("2011-08-27 11:00:00 GMT", "2011-10-30
15:30:00 GMT", "2011-10-30 15:45:00 GMT", "2012-06-22 10:00:00 GMT"))
If I use the following expression for one specific object of the list, I
get the result I expect:
x[[1]][as.Date(x[[1]])=="2011/10/30"][1]
[1] "2011-10-30 15:45:00 GMT"
Now if I write a for loop based on that expression to store the values
of interest in a data frame, the POSIXct values are coerced into numeric:
y<-data.frame()
for (i in 1:length(x)) {
y[i,"ID"]<-names(x[i])
y[i,"first"]<-x[[i]][as.Date(x[[i]])=="2011/10/30"][1]
y[i,"second"]<-x[[i]][as.Date(x[[i]])=="2011/10/30"][2]
}
ID first second
1 a 1319989500 1319990400
2 b 1319988600 1319989500
I am a bit confused about why that is.
I could coerce them back to POSIXct with another line of code, but is
there a better way? And is there a way to avoid using a for loop to
complete this task?
No need to say I am quite new to R, so apologies for any obvious mistake
or oversight.
Many thanks
A. Duranel, UCL Department of Geography
[[alternative HTML version deleted]]
______________________________________________ R-help at 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 at 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.
Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it.
Hi,
You can also try this:
?x1<-list(a=data.frame(x[["a"]]),b=data.frame(x[["b"]]))
library(plyr)
x2<-do.call(rbind.fill,x1)
colnames(x2)<-c("a","b")
x2first<-subset(x2,format(a,"%Y-%m-%d")=="2011-10-30" )
?x2second<-subset(x2,format(b,"%Y-%m-%d")=="2011-10-30" )
y<-data.frame(t(data.frame(a=x2first[,1],b=x2second[,2])))
colnames(y)<-c("first","second")
?y<-data.frame(ID=row.names(y),y)
y<-within(y,{first<-as.POSIXct(first);second<-as.POSIXct(second)})
?y
#? ID?????????????? first????????????? second
#a? a 2011-10-30 15:45:00 2011-10-30 16:00:00
#b? b 2011-10-30 15:30:00 2011-10-30 15:45:00
str(y)
#'data.frame':??? 2 obs. of? 3 variables:
# $ ID??? : Factor w/ 2 levels "a","b": 1 2
# $ first : POSIXct, format: "2011-10-30 15:45:00" "2011-10-30 15:30:00"
?#$ second: POSIXct, format: "2011-10-30 16:00:00" "2011-10-30 15:45:00"
A.K.
----- Original Message -----
From: Arnaud Duranel <arnaud.duranel.09 at ucl.ac.uk>
To: arun <smartpink111 at yahoo.com>
Cc: R help <r-help at r-project.org>
Sent: Monday, September 24, 2012 10:42 AM
Subject: [R] POSIXct coerced into numeric when filling a data frame
A.K., many thanks for your reply.
I have realised there was a mistake in my code, and that the example
could have been clearer; my apologies for this, the corrected code is below:
x<-list()
x[["a"]]<-as.POSIXct(c("2011-08-27 10:45:00 GMT", "2011-10-30
15:45:00 GMT", "2011-10-30 16:00:00 GMT", "2012-06-22 09:30:00 GMT", "2012-06-22 10:00:00 GMT"))
x[["b"]]<-as.POSIXct(c("2011-08-27 11:00:00 GMT", "2011-10-30
15:30:00 GMT", "2011-10-30 15:45:00 GMT", "2012-06-22 10:00:00 GMT"))
x[[1]][as.Date(x[[1]])=="2011/10/30"][1]
[1] "2011-10-30 15:45:00 GMT"
y<-data.frame()
for (i in 1:length(x)) {
+? y[i,"ID"]<-names(x[i]) +? y[i,"first"]<-x[[i]][as.Date(x[[i]])=="2011/10/30"][1] +? y[i,"second"]<-x[[i]][as.Date(x[[i]])=="2011/10/30"][2] + }
y
? ID? ? ? first? ? second 1? a 1319989500 1319990400 2? b 1319988600 1319989500 where I would need:
y
? ? ? ? ? ? ? ? ID? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? first? ? ? ? ? ? ? ? ? ? ? second 1? ? ? ? ? ? ? a? ? 2011-10-30 15:45:00? ? 2011-10-30 16:00:00 2? ? ? ? ? ? ? b? ? 2011-10-30 15:30:00? ? 2011-10-30 15:45:00 The POSIXct objects in my list do not have the same length; and I need to extract those POSIXct values that match a specific date (2011-10-30 in this example), while keeping them in POSIXct format. Many thanks Arnaud
On 24/09/2012 13:29, arun wrote:
HI,
Try this:
dat1<-do.call(data.frame,x)
dat1<-data.frame(ID=letters[1:4],dat1)
dat1
#? ID? ? ? ? ? ? ? first? ? ? ? ? ? ? second
#1? a 2011-08-27 10:45:00 2011-08-27 11:00:00
#2? b 2011-10-30 15:45:00 2011-10-30 15:30:00
#3? c 2011-10-30 16:00:00 2011-10-30 15:45:00
#4? d 2012-06-22 09:30:00 2012-06-22 10:00:00
? str(dat1)
#'data.frame':? ? 4 obs. of? 3 variables:
# $ ID? ? : Factor w/ 4 levels "a","b","c","d": 1 2 3 4
? #$ first : POSIXct, format: "2011-08-27 10:45:00" "2011-10-30 15:45:00" ...
? #$ second: POSIXct, format: "2011-08-27 11:00:00" "2011-10-30 15:30:00" ...
dat2<-dat1
? dat2$first<-as.Date(dat2$first,format="%Y-%m-%d %HH:MM:%SS")
? dat2$second<-as.Date(dat2$second,format="%Y-%m-%d %HH:MM:%SS")
? dat2
#? ID? ? ? first? ? second
#1? a 2011-08-27 2011-08-27
#2? b 2011-10-30 2011-10-30
#3? c 2011-10-30 2011-10-30
#4? d 2012-06-22 2012-06-22
? str(dat2)
#'data.frame':? ? 4 obs. of? 3 variables:
# $ ID? ? : Factor w/ 4 levels "a","b","c","d": 1 2 3 4
# $ first : Date, format: "2011-08-27" "2011-10-30" ...
# $ second: Date, format: "2011-08-27" "2011-10-30" ...
dat2<-within(dat2,ID<-as.character(ID))
subset(dat2,ID%in% c("a","b"))
#? ID? ? ? first? ? second
#1? a 2011-08-27 2011-08-27
#2? b 2011-10-30 2011-10-30
A.K.
----- Original Message -----
From: Arnaud Duranel <arnaud.duranel.09 at ucl.ac.uk>
To: r-help at r-project.org
Cc:
Sent: Monday, September 24, 2012 4:06 AM
Subject: [R] POSIXct coerced into numeric when filling a data frame
Hello
I have a list of POSIXct objects, from which I want to extract those
values that match a specific date, keeping them in POSIXct format. For a
specific date there is 0-2 matching values.
As an example (the actual list and objects are much longer):
x<-list()
x[["first"]]<-as.POSIXct(c("2011-08-27 10:45:00 GMT", "2011-10-30
15:45:00 GMT", "2011-10-30 16:00:00 GMT", "2012-06-22 09:30:00 GMT"))
x[["second"]]<-as.POSIXct(c("2011-08-27 11:00:00 GMT", "2011-10-30
15:30:00 GMT", "2011-10-30 15:45:00 GMT", "2012-06-22 10:00:00 GMT"))
If I use the following expression for one specific object of the list, I
get the result I expect:
x[[1]][as.Date(x[[1]])=="2011/10/30"][1]
[1] "2011-10-30 15:45:00 GMT"
Now if I write a for loop based on that expression to store the values
of interest in a data frame, the POSIXct values are coerced into numeric:
y<-data.frame()
for (i in 1:length(x)) {
? ? y[i,"ID"]<-names(x[i])
? ? y[i,"first"]<-x[[i]][as.Date(x[[i]])=="2011/10/30"][1]
? ? y[i,"second"]<-x[[i]][as.Date(x[[i]])=="2011/10/30"][2]
}
? ? ID? ? ? first? ? second
1? a 1319989500 1319990400
2? b 1319988600 1319989500
I am a bit confused about why that is.
I could coerce them back to POSIXct with another line of code, but is
there a better way? And is there a way to avoid using a for loop to
complete this task?
No need to say I am quite new to R, so apologies for any obvious mistake
or oversight.
Many thanks
A. Duranel, UCL Department of Geography
? ? ? [[alternative HTML version deleted]]
______________________________________________ R-help at 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. .