Well, first things first, I'm still a noob and am learning R. I've a a dataset with 0.9 million rows and 36 columns. Of these columns, a column, let's say DATE has dates in string format and an other column, let's say TZ has timezones as strings too.
What I'm trying to do is contract these two columns into one with type POSIXlt date, which has date, time, timezone. Here's my code for trying to get a vector of all the converted dates:
# Let's suppose my data exist in a variable "data" with dates in "DATE" column and timezones in "TZ"
indices <- NULL
dates <- NULL
zones <- unique (data$TZ)
for(i in seq_along(zones)){
indices <<- which(data$TZ==zones[i])
dates <<- c(dates, as.POSIXlt(data$DATE[indices], format = "%m/%d/%Y %H:%M:%S", tz = zones[i]))
}
Now, although there are ~1 million observations, it seems to do the job in 3-4 seconds. Only, that it "seems" to. The result I get is a list with NAs.
It does work when I try to convert a group individually, i.e., store result for every iteration in a different variable, or not run a for loop and do each iteration manually, storing each result in a different variable and, in the end, concatenate it all using c() function.
What am I doing wrong?
For anyone who might stumble here, I figured it.
You can't use c() on a POSIXlt object as it'll convert it into local timezone. (Not the reason for NAs but it's helpful.)
POSIXlt is stored as a list of different variables like mday, zone etc, due to which it's value cannot be used in a data frame element. Instead of POSIXlt, we can use POSIXct as that's internally represented as seconds from 1970-01-01.
Since we'll be replacing a data frame column with dates it's easier to do so with converting it into a tibble using dplyr::as_tibble() and then use dplyr::rbind() to combine the different results.
The reason of NAs being introduced is the lexical scoping in R. I used dates <<- c(dates, as.POSIXlt(data$DATE[indices], format = "%m/%d/%Y %H:%M:%S", tz = zones[i])) due to which, the value of i in zones[i] was NA or unknown.
So, the correct working code is -
dates <- NULL
for (i in seq_along(zones)) {
indices <- which(data$TZ==zones[i])
dts <- as.POSIXct(data$BGN_DATE[indices], format = "%m/%d/%Y %H%M", tz = zones[i])
dates <<- rbind(dates,as_tibble(dts))
}
#Further, to combine the dates into data frame
data <- arrange(data, TZ) %>% mutate(DATEandTime = dates$value) %>% select(-c("DATE","TZ"))
Related
I tried to convert a column containing time details from characters to a time type variable. The code works but converts all the characters into NA. The values in the column before the code looks like '06:34:10'
The code I am running is similar to this:
df$time <- as.POSIXct(df$time, format = "%H-%M-%S")
I want it to be a time variable so I can conduct analyses by grouping the different times an event occurred.
: instead to -
df$time <- as.POSIXct(df$time, format = "%H:%M:%S")
or
df$time <- strptime(df$time, format = "%H:%M:%S")
First time caller, longtime listener.
I am trying to solve two problems.
my function does not perform as anticipated.
I cannot figure out how to make a plot from date data
I have tried to approach my function problem from multiple angles but I am only making things harder than they need to be. The issue that I cannot overcome is that the date sequence I have created for the date range of the data set is not equal to the length of the data set columns.
For the y-axis of my plot, I want:
f(dates[x])= number of data set entries on or before dates[x],
Where dates[x] refers to a given date in the data set date range
I'm sure there is an easy solution but I cannot figure it out.
Note: I used to have a basic understanding of r but I am relearning after a long break, please use the simplest terms possible
# import data
data <- read.csv("https://raw.githubusercontent.com/washingtonpost/data-police-shootings/master/fatal-police-shootings-data.csv")
#
# coerce date column into date class
data$date <- as.POSIXlt.date(data$date)
#
# sequence of dates for date range of data set
dates <- seq(data$date[1], data$date[length(data$date)], by = "days")
#
# numeric vector for the number of days in the date range of data set
xx <- c(1:length(dates))
#
# function meant to return a numeric vector of the count of entries in the data set that occurred on or before a given date
# within the data set date range.
fun <- function(x){
sum(dates[x]<=data$date)
}
# This function returns a single value and not a vector as I'd expected.
# This plot is the objective. x = number of days in data set date range, y = number of entries in data set on or before date(x)
plot(xx,y=fun(xx))
Working with dates is a loaded topic. It is extremely powerful, but it pays to be careful. Here is my take:
data <- read.csv(paste0("https://raw.githubusercontent.com/washingtonpost/", # wrapped
"data-police-shootings/master/fatal-police-shootings-data.csv"))
library(anytime) ## helper package
data$date <- anydate(data$date) ## helper function not requiring format
Now we have a date type and you can do
data[ data$date <= anydate(20150110), ]
If you use the date on the x-axis it all works out correctly too.
That said, I tend to do all this inside of data.table objects, but that is more learning for you. Another day :) Keep it in mind -- the grouping aggregation and
filtering are absolutely worth it. And it is the fastest tool around.
I am seeking some insight into the code behavior below. Make a new POSIXct variable by generating the format string from another variable with a specific time zone and using that time zone to create the new POSIXct.
eventTime1Converted <- as.POSIXct(format(eventTime1, tz = "GMT", usetz = TRUE), tz = "GMT")
Strangely, these two variables don't end up being equal:
> eventTime1Converted == eventTime1
[1] FALSE
In particular my 'GMT' timestamped variable seems to be 'less than' my 'EST' timestamped variable (the original variable). So it seems like when the numeric portion is equal, the timezones are then compared? If this is the case, what's the 'correct' way to check equality on two POSIXct variables? To compare their as.numeric values?
I have a R dataframe which have sequence of dates. I want to create a dataframe from the existing one which consists of one month prior dates.
For example let x be the initial dataframe
x = data.frame(dt = c("28/02/2000","29/02/2000","1/03/2000","02/03/2000"))
My required dataframe y would be
y = c("28/01/2000","29/01/2000","1/02/2000","02/02/2000")
The list is quite big so I don't want looping. I have created a inline function which works fine when I give individual dates.
datefun <- function(x) seq(as.Date(strptime(x,format = "%d/%m/%Y")), length =2, by = "-1 month")[2]
datefun("28/02/2000") gives "28/01/2000" as an output
But while I use it inside R apply it gives random numerical values.
apply(x,1,function(x) datefun(x))
The output for this is
[1] 10984 10985 10988 10989
I don't know from where these numbers are getting generated, am I missing something.
You should not use apply since the result will be returned as a matrix. Matrices in R cannot store values of class Date. You have to use lapply instead. This returns a list of results. These results can be combined with Reduce and c to create a Date vector.
Reduce(c, lapply(x$dt, datefun))
# [1] "2000-01-28" "2000-01-29" "2000-02-01" "2000-02-02"
I believe that R internally is storing your dates as time elapsed since the UNIX epoch, which is January 1, 1970. You can easily view your updated dates as readable strings using as.Date with an apporpriate origin, e.g.
y <- apply(x,1,function(x) datefun(x))
as.Date(y, origin='1970-01-01')
[1] "2000-01-28" "2000-01-29" "2000-02-01" "2000-02-02"
The gist here is that the numerical output you saw perhaps misled you into thinking that your date information were somehow lost. To the contrary, the dates are stored in a numerical format, and it is up to you to tell R how you want to view that information as dates.
Demo
You could also skip your function with lubridate:
require(lubridate)
format(dmy(x$dt) %m+% months(-1),"%d/%m/%Y")
I have run into an issue I do not understand, and I have not been able to find an answer to this issue on this website (I keep running into answers about how to convert dates to numeric or vice versa, but that is exactly what I do not want to know).
The issue is that R converts values that are formatted as a date (for instance "20-09-1992") to numeric values when you assign them to a matrix or data frame.
For example, we have "20-09-1992" with a date format, we have checked this using class().
as.Date("20-09-1992", format = "%d-%m-%Y")
class(as.Date("20-09-1992", format = "%d-%m-%Y"))
We now assign this value to a matrix, imaginatively called Matrix:
Matrix <- matrix(NA,1,1)
Matrix[1,1] <- as.Date("20-09-1992", format = "%d-%m-%Y")
Matrix[1,1]
class(Matrix[1,1])
Suddenly the previously date formatted "20-09-1992" has become a numeric with the value 8298. I don't want a numeric with the value 8298, I want a date that looks like "20-09-1992" in date format.
So I was wondering whether this is simply how R works, and we are not allowed to assign dates to matrices and data frames (somehow I have managed to have dates in other matrices/data frames, but it beats me why those other times were different)? Is there a special method to assigning dates to data frames and matrices that I have missed and have failed to deduce from previous (somehow successful) attempts at assigning dates to data frames/matrices?
I don't think you can store dates in a matrix. Use a data frame or data table. If you must store dates in a matrix, you can use a matrix of lists.
Matrix <- matrix(NA,1,1)
Matrix[1,1] <- as.list(as.Date("20-09-1992", format = "%d-%m-%Y"),1)
Matrix
[[1]]
[1] "1992-09-20"
Edited: I also just re-read you had this issue with data frame. I'm not sure why.
mydate<-as.Date("20-09-1992", format = "%d-%m-%Y")
mydf<-data.frame(mydate)
mydf
mydate
1 1992-09-20
Edited: This has been a learning experience for me with R and dates. Apparently the date you supplied was converted to number of days since origin. Origin is defined as Jan 1st,1970. To convert this back to a date format at some point
Matrix
[,1]
[1,] 8298
as.Date(Matrix, origin ="1970-01-01")
[1] "1992-09-20"
try the following: First specify your date vector & then use
rownames(mat) <- as.character(date_vector)
the dates will appear as a text.
This happens mostly when we are loading Excel Workbook
You need to add detectDates = TRUE in the function
DataFrame <- read.xlsx("File_Nmae", sheet = 3, detectDates = TRUE)