For annual data (such as Annual Income Statements), I would like to keep xts format but I need to convert the index of the table to "only year". There are yearmon and yearqtr classes but I did not find "year" only class to work with xts.
# IS is annual reports of incomes. time(IS) is POSIXct.
library(quantmod)
IS <- viewFin(get(getFin("IBM")), "IS", "A") # Download data
IS <- as.xts(t(IS)) # Convert to xts
time(IS) <- as.yearqtr(time(IS)) ## works to have quarterly index
time(IS) <- as.yearmon(time(IS)) ## works to have monthly index
time(IS) <- ????(time(IS)) ## To have yearly index with xts class
What is the best solution? Thank you.
It would be helpful if you explained why you need to have the index as "year" only. Xts has an indexFormat command that allows you to control how dates are displayed, and while I've never used it I assume it will allow you to display only the year of any given index entry.
A more extreme solution would be to convert every date to the first of the year in that year. Here's some code to help do this:
first.of.year <- function(x) # Given a date, returns the first day of that year
return(as.Date(paste(year(as.Date(x)),"-01-01", sep="")))
index(x) <- first.of.year(index(x))
Related
I know this question has probably been answered in different ways, but still struggling with this. I am working with a dataset where the dates format for date1 is '2/1/2000', '5/12/2000', '6/30/2015' where the class() is character. And the second column of dates date2 in the format '2015-07-06', '2015-08-01', '2017-10-09' where the class() is "POSIXct" "POSIXt" .
I am attempting to standardize both columns so I can compute the difference in days between them using something like this
abs(difftime(date1 ,date2 , units = c("days")))
I have tried numerous ways in converting the first date1 into the same class using strtime, lubridate etc. What's the best way to move forward for me to be able to standardize both and compute the difference in days?
sample data
x <- c('2/1/2000', '5/12/2000', '6/30/2015')
y <- as.POSIXct(c('2015-07-06', '2015-08-01', '2017-10-09'))
code
#make both posixct
x2 <- as.POSIXct(x, format = "%m/%d/%Y")
abs(x2 - y)
# Time differences in days
# [1] 5633.958 5559.000 832.000
NDVI RasterStack
I have a 15day 1981-2015 NDVI RasterStack.
I need to calculate monthly NDVI with the 15d data.
I want to know how to calculate mean of the same names MM raster into a new monthly 1981-2015 rasterstack
I appreciate for your help! Thank you very much.
names XYYYY.MM.DD
I have recently been working on the same solution to you problem, this should work for you also.
You want to create a separate variable that contains the dates from your layer names.
#this removes the "X" character from the name leaving only the dates
layer_name <- sub('.', '', names(NDVI_stack))
#install.packages("lubridate")
library(lubridate)
layer_name <- ymd(layer_name)
#Create an indices to prepare it for stackApply, which takes the means for all the days of the month within each year.
indices <- format(as.Date(layer_name, format = "%Y.%m.%d"), format = "%Y.m")
NDVI_mean <- stackApply(NDVI_stack, indices, mean)
I want to automatise a code that calculates transport times. I would like that the code gives me 4 months that you can choose out of a big readout from a year and splits up the last month in its four weeks and just describes the data subsets (describing is not the problem).
Generating subsets from a dataset for chosen months is not the problem because I can define the months.
But where I struggle is the 3/4 weeks of the last month. I need to identify them automatically and after that generate the subsets. (I hope that generating subsets should be easier after identifying.)
I can give you a little mock-up of my data.
dates <- as.Date(c("2019-01-07", "2019-01-08", "2019-01-09",
"2019-01-15", "2019-01-21"))
number <- c(12,13,14,15,20)
df <- data.frame(number, dates)
The original df contains of 60 variables but I believe this simple mockup can provide enough info for the task.
I am pretty new to r, I have no idea how to solve the problem, I will show you how I solved it with the months, but as said, in this case they are defined.
function(data = df, m1 = "01" , m2 = "02") {
Monat1 <- subset(data, format.Date(dates , "%m") == m1)
Thank you for helping me out a bit.
you can use the function strftime
strftime(df$dates, format = "%W")
in rstudio use
?strftime()
to see all the different values you can extract from a date or POSIXCT object
You can do it using base R and lubridate
Data
dates <- as.Date(c("2019-01-07", "2019-01-08", "2019-01-09",
"2019-01-15", "2019-01-21"))
number <- c(12,13,14,15,20)
df <- data.frame(number, dates)
str(df)
Answer
library(lubridate)
df$condition <- ifelse(month(df$dates) == month(Sys.Date())-1,week(df$dates),"-")
condition will check if the date is less than a month ago or not and if yes it will give you week number for that particular value
I have a CSV file containing data as follows-
date, group, integer_value
The date starts from 01-January-2013 to 31-October-2015 for the 20 groups contained in the data.
I want to create a time series for the 20 different groups. But the dates are not continuous and have sporadic gaps in it, hence-
group4series <- ts(group4, frequency = 365.25, start = c(2013,1,1))
works from programming point of view but is not correct due to gaps in data.
How can I use the 'date' column of the data to create the time series instead of the usual 'frequency' parameter of 'ts()' function?
Thanks!
You could use zoo::zoo instead of ts.
Since you don't provide sample data, let's generate daily data, and remove some days to introduce "gaps".
set.seed(2018)
dates <- seq(as.Date("2015/12/01"), as.Date("2016/07/01"), by = "1 day")
dates <- dates[sample(length(dates), 100)]
We construct a sample data.frame
df <- data.frame(
dates = dates,
val = cumsum(runif(length(dates))))
To turn df into a zoo timeseries, you can do the following
library(zoo)
ts <- with(df, zoo(val, dates))
Let's plot the timeseries
plot.zoo(ts)
I have several dates of measurement for the same specimen. I'm trying to figure out the first day I have and the 2nd, 3rd, 4th...up to 6th day.
here is the data
First I took data$start and split it
#split timestamp into separate date and time vars
temp<-strsplit(as.character(data$start), " ")
mat<-matrix(unlist(temp), ncol=2, byrow=TRUE)
df<-as.data.frame(mat)
colnames(df)<-c("date", "time")
data<-cbind(df, data)
then
data$date<-as.Date(data$date, "%Y-%m-%d")
data$dob <- ave(as.numeric(data$date), data$mcode, FUN = min)
data$dob <- data$dob - 1
data$pnday <- as.numeric(data$date) - data$dob
Both pnd and dob columns have an NA -- sorry if this is silly, any ideas?
I'm new to working with dates/times in R
If you format your data as an xts, it will automatically be ordered by date from first to last. To do this, you'll need to make your timestamp readings into POSIXct instances and pass them to the order.by parameter of its constructor. I hope this helps you. If you need further help, please leave a comment, and I'll dig into your data.