4

I am working with a data frame in R labeled "mydata". The first column, labled "ts" contains unix timestamp fields. I'd like to convert these fields to days of the week.

I've tried using strptime and POSIXct functions but I'm not sure how to execute them properly:

> strptime(ts, "%w")

--Returned this error:

"Error in as.character(x) : cannot coerce type 'closure' to vector of type 'character'"

I also just tried just converting it to human-readable format with POSIXct:

as.Date(as.POSIXct(ts, origin="1970-01-01"))

--Returned this error:

"Error in as.POSIXct.default(ts, origin = "1970-01-01") : do not know how to convert 'ts' to class “POSIXct”"

Update: Here is what ended up working for me:

> mydata$ts <- as.Date(mydata$ts)

then

> mydata$ts <- strftime( mydata$ts , "%w" )
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
pas
  • 53
  • 1
  • 5
  • 1
    Include sample data using `dput`. – Thomas Jun 09 '13 at 06:31
  • 1
    `ts` is the function that creates time series objects. Perhaps you meant to write `strptime(mydata$ts, "%w")` – James Jun 09 '13 at 06:37
  • Tried that and it output 10k rows that look like this: _[9997] "2013-06-09" "2013-06-09" "2013-06-09"_ but it doesn't appear like the actual contents of the data frame changed. – pas Jun 09 '13 at 06:40
  • @pas I think you will want to use `strftime` – James Jun 09 '13 at 06:49
  • @pas you have to assign the output to an object, in this case `mydata$ts <- strftime( mydata$ts , ... )` – Simon O'Hanlon Jun 09 '13 at 07:08
  • After trying your suggestion @SimonO101, I get this **"Error: '...' used in an incorrect context"** --Running that last command James suggested froze my computer because it's such a large dataset (27mb) – pas Jun 09 '13 at 07:19
  • @pas the `...` was a placeholder for the other arguments you need to type in yourself! Try `mydata$ts <- strftime( mydata$ts , "%w" )` and may I suggest you familiarise yourself with some of the tutorials under the [**info**](http://stackoverflow.com/tags/r/info) page of the `r` tag. – Simon O'Hanlon Jun 09 '13 at 07:20
  • @SimonO101 sorry! I misunderstood-thought it had some connection to the ‘...’ argument. Could I assign the output back into the ts column? I'm really not familiar with R -- trying to look into how to assign output to objects now... – pas Jun 09 '13 at 07:25
  • Thanks @SimonO101 -- I'll check it out. Also when I try that, I get *Error in as.POSIXlt.numeric(x, tz = tz) : 'origin' must be supplied* -- I'll keep looking and thanks for the help – pas Jun 09 '13 at 07:26
  • @pas that's *exactly* what the `mydata$ts <-` does. – Simon O'Hanlon Jun 09 '13 at 07:28

1 Answers1

9

No need to go all the way to strftime when POSIXlt gives you this directly, and strftime calls as.POSIXlt.

wday <- function(x) as.POSIXlt(x)$wday

wday(Sys.time()) # Today is Sunday
## [1] 0

There is also the weekdays function, if you want character rather than numeric output:

weekdays(Sys.time())
## [1] "Sunday"
Matthew Lundberg
  • 42,009
  • 6
  • 90
  • 112