开发者

Aggregating By Time

开发者 https://www.devze.com 2023-04-04 16:02 出处:网络
I want to aggregate against the Time column that is read in as a string and then subset the output but I can\'t convert the output tmp$Time to Time value.

I want to aggregate against the Time column that is read in as a string and then subset the output but I can't convert the output tmp$Time to Time value.

Data was created by reading in a csv file.

> head(Data)
                 Time  F   Z
1 2011-09-09-06.54.00  1 489
2 201开发者_StackOverflow1-09-09-06.54.00  3 678
3 2011-09-09-06.54.00  6 890
4 2011-09-09-07.54.00  8 345
5 2011-09-09-07.54.00 10 567

> strptime(Data$Time, format="%Y-%m-%d-%H.%M.%S" )
[1] "2011-09-09 06:54:00" "2011-09-09 06:54:00"
[3] "2011-09-09 06:54:00" "2011-09-09 07:54:00"
[5] "2011-09-09 07:54:00"

> tmp <- aggregate(Data$Z ~ Data$Time,Data, sum)

> head(tmp)
            Data$Time Data$Z
1 2011-09-09-06.54.00   2057
2 2011-09-09-07.54.00    912

> strptime(tmp[1], format="%Y-%m-%d-%H.%M.%S" )
Data$Time 
       NA 

I am using aggregate against the Time value before converting it because I can't get it to work against Time when it is a time.


This seems to work

Data <- data.frame(Time = c("2011-09-09 06:54:00", "2011-09-09 06:54:00",
                            "2011-09-09 06:54:00", "2011-09-09 07:54:00",
                            "2011-09-09 07:54:00"),
                   F = c(1, 3, 6, 8, 10),
                   Z = c(489, 678, 890, 345, 567)   )
tmp <- aggregate(Data$Z ~ Data$Time,Data, sum)

strptime(tmp[,1], format="%Y-%m-%d %H:%M:%S" )

producing

[1] "2011-09-09 06:54:00" "2011-09-09 07:54:00"

I made two changes to your final line:

  • tmp[,1] rather than tmp[1]
  • format="%Y-%m-%d %H:%M:%S" rather than format="%Y-%m-%d-%H.%M.%S"
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号