I am trying to change my date within my dataframe into the correct format within R. (m/d/y to the correct yyyy-mm-dd).
I have data that looks like this
Date Time pH
1 1/4/1981 9:00 3.9
2 1/8/1981 8:30 3.9
etc
The name of my data frame I am working in is data.cat.AC
I tried
data.cat.AC[,1]$Date <- as.Date(data.cat.AC[,1]$Date, "%Y/%m/%d")
...but this did not work.
I am getting the error,
$ operator is invalid for atomic vectors
Any tips or pointers on what I am doing wrong?
When you use as.Date, you should not enter the format that you want as output. Instead enter the format as it is in the data.
as.Date("1/4/1981", format="%m/%d/%Y")
[1] "1981-01-04"
We got lucky in this case in that your desired output happens to be the default output. But for learning purposes, let's say you wanted the format "dd:mm:YYYY". After converting to Date format as we did above, we would use:
format(x2, "%m:%d:%Y")
[1] "01:04:1981"
Related
I've tried everything in this thread as.Date returning NA while converting from 'ddmmmyyyy' to try and sort my problem.
I'm using these commands to turn a factor into a date:
cohort$doi <- as.Date(cohort$doi, format= "%Y/%m/%d")
All my dates are currently in the format: YYYY-MM-DD, so as far as I'm aware the above should work
I used this code yesterday to convert all my dates for various variables from a factor to a date. It worked yesterday and everything was fine. Today I opened my script and imported in my data, ran this command and viewed my data but all of the dates now say NA.
I've tried everything from previous threads (I looked at a few more than just the one I linked above) but nothing has so far worked. I'm not sure what to do now
Example of what doi column looks like:
1970-01-01
1970-02-02
1970-03-03
1970-04-04
The column is currently classed as an factor. And when I do the code I used above, the column is defined as a date but all the dates now say NA
Other than closing R and opening it up again for today, I've done nothing else.
If you read the documentation for as.Date you will note the default format is %Y-%d-%m or %Y/%d/%m:
The default formats follow the rules of the ISO 8601 international standard which expresses a day as "2001-02-03".
In your code you have specified your dates are formatted by slashes, but your sample data shows they are formatted in the default format used by as.Date:
doi <- as.factor(c("1970-01-01",
"1970-02-02",
"1970-03-03",
"1970-04-04"))
as.Date(doi) # default format %Y-%m-%d
[1] "1970-01-01" "1970-02-02" "1970-03-03" "1970-04-04"
as.Date(doi, format = "%Y/%m/%d") # incorrect specification of your date format
[1] NA NA NA NA
as.Date("1970/01/01") # also a default format
[1] "1970-01-01"
Note: as.Date accepts character strings, factors, logical NA and objects of classes "POSIXlt" and "POSIXct".
I am attempting to convert a column in my data set from fctr to date format. The current column has data formatted as follows: "01/01/14. 01:00 Am." Ideally I would like to create a column for day and then a column for time as well. There are periods following the day and the time which is another issue I am facing. So far I have attempted to use lubridate to create a new column of data but I get the error "All formats failed to parse. No formats found." Any help would be greatly appreciated, thank you.
test <- fourteen %>%
mutate(When = mdy_hms(V3))
View(test)
If your date factor literally has levels that look like 01/01/14. 01:00 Am. including two periods and a space between the first period and the first hour digits and a space between the minutes and the am/pm designation, and all the dates are in this format, then the following should work:
... mutate(When = as.POSIXct(V3, format="%m/%d/%y. %H:%M %p.")) ...
In particular, the following standalone testcase works fine:
as.POSIXct(factor("01/01/14. 01:00 Am."), format="%m/%d/%y. %H:%M %p.")
For more information on the format argument being used here, see the R help page for the function strftime.
I have a problem with some date variables in my data. I already checked other similar questions here but I couldn't find the answer.
I have a very long dataset and some date vectors. The data was originally in stata format, I've tried to change them into R date format with:
as.Date(example$dstart)
which seems to work, after checking the class of the vector; but then I realised that apparently some cases are not in the standard unambiguous format that R requires, I realised when I was trying to convert "." into NAs, when I got this message
Error in charToDate(x) :
character string is not in a standard unambiguous format
This is an example of the data that I have:
head(sample)
dstart dstart2 dleave Ind
2005-03-20 <NA> 2005-11-19 1
2005-10-27 2006-07-07 2005-11-15 2
2000-02-29 2008-04-16 2005-03-02 3
2003-09-10 2007-07-23 2005-04-05 4
2004-04-24 2006-02-28 2005-10-17 5
2005-08-16 <NA> 2005-08-20 6
I presume that there are a few cases in the wrong format, but I don't know how to identify those cases.
Could you please advice me how to change the format of the of the date vector into an R format? I've tried this but it doesn't solve my problem.
as.Date(example$dstart, format = "%Y/%m/%d")
This has caused me some problem in my analysis when trying to sort by date, some dates are sorted before when they are obviously posterior.
A sample of the data
Your date format specification is using "/" instead of "-". If all your data is like your example, this should do it:
as.Date(example$dstart, format = "%Y-%m-%d")
Format a date from CSV into a date R can use.
I have a time series data file. I want to load it into R, and cast the Date column to be something usable by R. Can I specify the input format to as.Date(), or use another function that will correctly cast a Date such as 1/1/14?
In other languages I'm used to passing in a format string that tells the caster exactly how to format it, e.g. toDate('1/1/1', '%d/%m/%y'). I haven't found this function yet for R.
my_time_series.csv
Date Value
1/1/14 123
1/2/14 128.56
1/3/14 129.14
1/4/14 130.13
1/5/14 137.97
1/6/14 141.05
1/7/14 141.35
1/8/14 142.14
1/9/14 142.14
1/10/14 149.89
Now I can import it into R:
$ R
> dat = read.csv("time series test.csv", header = TRUE)
> dat
Date Value
1 1/1/14 123.0000
2 1/2/14 128.5693
3 1/3/14 129.1474
4 1/4/14 130.1361
5 1/5/14 137.9758
6 1/6/14 141.0548
7 1/7/14 141.3517
8 1/8/14 142.1449
9 1/9/14 142.1479
10 1/10/14 149.8912
Ok now I need to format those dates as actual dates. The as.Date() casting function looks promising, but returns an incorrect date:
> as.Date('1/10/14')
[1] "0001-10-14"
So I searched for whether I can specify an input format for as.Date(), but it only has a second parameter format for the output format.
I tried to work around this in Excel before saving the CSV, but it doesn't have any formats that seem to work by default with as.Date().
I'm an idiot, was just typing in the wrong formatter. This works fine.
> as.Date('1/10/14', '%m/%d/%y')
[1] "2014-01-10"
So if I have any arbitrary date format in the future, just rearrange the format string.
I have a data set in R that contains a column of dates in the format yyyy/mm/dd. I am trying to use as.Date to convert these dates to date objects in R. However, I cannot seem to find the correct argument for origin to input into as.Date. The following code is an example of what I have been trying. I am using a CSV file from Excel, so I used origin="1899/12/30 based on other sites I have looked at.
> as.Date(2001/04/26, origin="1899/12/30")
[1] "1900-01-18"
However, this is not working since the input date 2001/04/26 is returned as "1900-01-18". I need to convert the dates into date objects so I can then convert the dates into julian dates.
You can either is as.Date with a numeric value, or with a character value. When you type just 2001/04/26 into R, that's doing division and getting 19.24 (a numeric value). And numeric values require an origin and the number you supply is the offset from that origin. So you're getting 19 days away from your origin, ie "1900-01-18". A date like Apr 26 2001 would be
as.Date(40659, origin="1899-12-30")
# [1] "2011-04-26"
If your dates from Excel "look like" dates chances are they are character values (or factors). To convert a character value to a Date with as.Date() you want so specify a format. Here
as.Date("2001/04/26", format="%Y/%m/%d")
# [1] "2001-04-26"
see ?strptime for details on the special % variables. Now if you're read your data into a data.frame with read.table or something, there's a chance your variable may be a factor. If that's the case, you'll want do convert to character with'
as.Date(as.character(mydf$datecol), format="%Y/%m/%d")