Removing duplicate dates based on another column in R

I have a timer with several entries for several hours.

                 date  wd  ws temp sol octa pg  mh daterep
1 2007-01-01 00:00:00 100 1.5  9.0   0    8  D 100   FALSE
2 2007-01-01 01:00:00  90 2.6  9.0   0    7  E  50    TRUE
3 2007-01-01 01:00:00  90 2.6  9.0   0    8  D 100    TRUE
4 2007-01-01 02:00:00  40 1.0  8.8   0    7  F  50   FALSE
5 2007-01-01 03:00:00  20 2.1  8.0   0    8  D 100   FALSE
6 2007-01-01 04:00:00  30 1.0  8.0   0    8  D 100   FALSE

I need to go to the time series with one record per hour, taking a record with a minimum value of mh, where there are several records. (Thus, in the above data, my second record should be row 2, and row 3 should be deleted.) I am working on both approaches: I select what I want in the new data framework and delete what I don't want in the existing one, but not getting it. Thank you for your help.

+3
source share
2 answers

You can sort your data with dateand mhwith plyr::arrangeand then remove duplicates:

df <- read.table(textConnection("

               date    wd  ws temp sol octa pg  mh daterep
'2007-01-01 00:00:00' 100 1.5  9.0   0    8  D 100   FALSE
'2007-01-01 01:00:00'  90 2.6  9.0   0    7  E  50    TRUE
'2007-01-01 01:00:00'  90 2.6  9.0   0    8  D 100    TRUE
'2007-01-01 02:00:00'  40 1.0  8.8   0    7  F  50   FALSE
'2007-01-01 03:00:00'  20 2.1  8.0   0    8  D 100   FALSE
'2007-01-01 04:00:00'  30 1.0  8.0   0    8  D 100   FALSE

"), header = TRUE)

library(plyr)
df <- arrange(df, date, mh)
df <- df[!duplicated(df$date), ]
df
#                  date  wd  ws temp sol octa pg  mh daterep
# 1 2007-01-01 00:00:00 100 1.5  9.0   0    8  D 100   FALSE
# 2 2007-01-01 01:00:00  90 2.6  9.0   0    7  E  50    TRUE
# 4 2007-01-01 02:00:00  40 1.0  8.8   0    7  F  50   FALSE
# 5 2007-01-01 03:00:00  20 2.1  8.0   0    8  D 100   FALSE
# 6 2007-01-01 04:00:00  30 1.0  8.0   0    8  D 100   FALSE
+1
source

flodel, R , date DateTimeClass:

d <- read.table(text = "
               date    wd  ws temp sol octa pg  mh daterep
'2007-01-01 00:00:00' 100 1.5  9.0   0    8  D 100   FALSE
'2007-01-01 01:00:00'  90 2.6  9.0   0    7  E  50    TRUE
'2007-01-01 01:00:00'  90 2.6  9.0   0    8  D 100    TRUE
'2007-01-01 02:00:00'  40 1.0  8.8   0    7  F  50   FALSE
'2007-01-01 03:00:00'  20 2.1  8.0   0    8  D 100   FALSE
'2007-01-01 04:00:00'  30 1.0  8.0   0    8  D 100   FALSE
", header = TRUE)

d$date <- as.POSIXct(d$date)

d <- d[order(d$date, d$mh), ]
d[!duplicated(d$date), ]

                 date  wd  ws temp sol octa pg  mh daterep
1 2007-01-01 00:00:00 100 1.5  9.0   0    8  D 100   FALSE
2 2007-01-01 01:00:00  90 2.6  9.0   0    7  E  50    TRUE
4 2007-01-01 02:00:00  40 1.0  8.8   0    7  F  50   FALSE
5 2007-01-01 03:00:00  20 2.1  8.0   0    8  D 100   FALSE
6 2007-01-01 04:00:00  30 1.0  8.0   0    8  D 100   FALSE
0

All Articles