-1

Here is an example of a subset data in .csv files. There are three columns with no header. The first column represents the date/time and the second column is load [kw] and the third column is 1= weekday, 0 = weekends/ holiday.

9/9/2010 3:00   153.94  1
9/9/2010 3:15   148.46  1

I would like to program in R, so that it selects the first and second column within time ranges from 10:00 to 20:00 for all weekdays (when the third column is 1) within a month of September and do not know what's the best and most efficient way to code.

code dt <- read.csv("file", header = F, sep=",") 

#Select a column with weekday designation = 1, weekend or holiday = 0 

y <- data.frame(dt[,3]) 

#Select a column with timestamps and loads 

x <- data.frame(dt[,1:2]) 
t <- data.frame(dt[,1]) 

#convert timestamps into readable format 

s <- strptime("9/1/2010 0:00", format="%m/%d/%Y %H:%M") 
e <- strptime("9/30/2010 23:45", format="%m/%d/%Y %H:%M") 
range <- seq(s,e, by = "min") 
df <- data.frame(range)
Andrie
  • 176,377
  • 47
  • 447
  • 496
Python_R
  • 41
  • 2
  • 9
  • 2
    What have you tried? It sounds like you're asking us to write your code for you. Take a look [here](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). It will make it much easier to answer your questions. – Justin Aug 10 '12 at 18:39
  • Somehow I kept having a problem posting my code. Here is what I have so far. `code` dt <- read.csv("file", header = F, sep=",") #Select a column with weekday designation = 1, weekend or holiday = 0 y <- data.frame(dt[,3]) #Select a column with timestamps and loads x <- data.frame(dt[,1:2]) t <- data.frame(dt[,1]) #convert timestamps into readable format s <- strptime("9/1/2010 0:00", format="%m/%d/%Y %H:%M") e <- strptime("9/30/2010 23:45", format="%m/%d/%Y %H:%M") range <- seq(s,e, by = "min") df <- data.frame(range) – Python_R Aug 10 '12 at 19:01
  • Related to [comparing time portion of POSIXct in R](http://stackoverflow.com/q/11853524/271616). – Joshua Ulrich Aug 10 '12 at 19:10

1 Answers1

2

OP ask for "best and efficient way to code" this without showing "inefficient code", so @Justin is right.

It's seems that the OP is new to R (and it's officially the summer of love) so I give it a try and I have a solution (not sure about efficiency..)

index <- c("9/9/2010 19:00", "9/9/2010 21:15", "10/9/2010 11:00", "3/10/2010 10:30")
index <- as.POSIXct(index, format = "%d/%m/%Y %H:%M")

set.seed(1)
Data <- data.frame(Date = index, load = rnorm(4, mean = 120, sd = 10), weeks = c(0, 1, 1, 1))

## Data
##                  Date   load weeks
## 1 2010-09-09 19:00:00 113.74     0
## 2 2010-09-09 21:15:00 121.84     1
## 3 2010-09-10 11:00:00 111.64     1
## 4 2010-10-03 10:30:00 135.95     1


cond <- expression(format(Date, "%H:%M") < "20:00" & 
                   format(Date, "%H:%M") > "10:00" & 
                   weeks == 1 & 
                   format(Date, "%m") == "09")

subset(Data, eval(cond))
##                  Date   load weeks
## 3 2010-09-10 11:00:00 111.64     1
Community
  • 1
  • 1
dickoa
  • 18,217
  • 3
  • 36
  • 50
  • Thank you so much! Sorry I just started using R and do not want to make a fool of myself by posting my codes. I am really bad at this. – Python_R Aug 10 '12 at 20:13