bhinks
bhinks

Reputation: 71

Creating a Data Frame based on day of week from a Data Frame containing several months of data

I am just starting with R and hoping to get some help. I have a set of data in a file as follows:

20111001|37|9|9
20111002|48|6|11
...
20111031|42|6|8


I have loaded in the file using the following:

    myClasses <- c( "character", "numeric", "numeric", "numeric" )
    df <- read.table( "inputfile", FALSE, "|", colClasses=myClasses )
    names( df ) <- c( "datestamp", "cnt1", "cnt2", "cnt3" )
    df$datestamp <- as.Date( df$datestamp, "%Y%m%d" )


How can I create another data frame containing only the data from a certain weekday (i.e., a dataframe containing all the same information as df but only for Fridays)?

Upvotes: 0

Views: 802

Answers (2)

Ramnath
Ramnath

Reputation: 55735

Here is another approach using lubridate.

# READ DATA
dat <- read.table(text = 
"20111001|37|9|9
 20111002|48|6|11
 20111031|42|6|8",
 sep = "|", header = F, 
)

# FORMAT DATE COLUMN
library(lubridate)
dat <- transform(dat, V1 = ymd(V1))

# CREATE SUBSET
subset(dat, wday(V1, label = T) == 'Sat')

Upvotes: 0

Brian Diggs
Brian Diggs

Reputation: 58875

If you are doing it once (just for one day), you can use

fridays <- df[format(df$datestamp, "%A") == "Friday",]

Your example data does not have any days on Friday, so this is an empty data frame.

If you are going to subset for more days, it might be easier to make a day of week column, and then subset on that.

df$day.of.week <- format(df$datestamp, "%A")
fridays <- df[df$day.of.week == "Friday",]

Going into even further speculation, if you plan to do the same things to each subset of data, look into split-apply-combine approaches for the data (various functions related to apply and the plyr package, among others).

Upvotes: 1

Related Questions