This question already has answers here:
What are the "standard unambiguous date" formats for string-to-date conversion in R?
(8 answers)
Closed 9 years ago.
I need your help to figure out the following problem-
I am trying to convert a date column from string to actual date format. I have tried using as.Date
However, it is showing an error message:
Error in charToDate(x) :
character string is not in a standard unambiguous format
the date column I have in csv file is like this:
Date
03/17/2003
05/31/2003
09/06/2003
10/18/2003
07/15/2003
09/19/2003
The problem is some of the dates are in string and some are in actual date format. I have tried to format it from excel - didn't work
Tried to copy and paste it to notepad and then import it again - didn't work either.
You need to learn about the help system in R. One brief look at help(as.Date) may have told you about the format argument:
R> dt <- c("03/17/2003", "05/31/2003", "09/06/2003")
R> as.Date(dt, "%m/%d/%Y")
[1] "2003-03-17" "2003-05-31" "2003-09-06"
R>
Edit: These days we also have a helper package that does the format-finding for you:
> dt <- c("03/17/2003", "05/31/2003", "09/06/2003")
> anytime::anydate(dt)
[1] "2003-03-17" "2003-05-31" "2003-09-06"
>
This works for datetimes (using anytime()) and dates.
Related
This question already has answers here:
Get date column from numpy loadtxt()
(2 answers)
Closed 4 years ago.
I have an array of dates in the format ('yyyy-mm-dd') and another array of integers numbers, each corresponding to a value in the date array. But, when I tried to plot the graph using:
matplotlib.pyplot.plot(dates, values, label='Price')
It gives the error:
ValueError: could not convert string to float: '2017-07-26'
How do I fix this error?
Your dates are strings, convert them to datetime objects first.
import datetime
x = [datetime.datetime.strptime(date, "%Y-%m-%d") for date in dates]
This question already has answers here:
Changing YYYYMMDD to MM/DD/YYYY
(5 answers)
Closed 4 years ago.
What formula can I use to transform the string into a date value that will appear as 01/01/2018?
If you are trying to do this from a formula into another cell, and not in situ (For which, use "Text to Columns" as per Jeeped's answer), you can either combine DATE and MID, or use REPLACE and convert with +0 or DATEVALUE:
=DATE(MID(A1,1,4), MID(A1,5,2), MID(A1,7,2))
OR
=REPLACE(REPLACE(A1,7,0,"-"),5,0,"-")+0
OR
=DATEVALUE(REPLACE(REPLACE(A1,7,0,"-"),5,0,"-"))
(Where A1 is the date to convert)
The first formula just cuts the number up into 2018 01 01 and uses those as Year, Month and Day. The second 2 work by first Inserting (i.e. REPLACE 0 characters) a hyphen at position 7 ("201801-01") and then at position 5 ("2018-01-01") and converting the string back to a number/date.
Use Data, TextToColumns, Fixed width, Date: YMD, Finish. Possibly Date: YDM depending on your string date format (you provided an ambiguous example).
use the code below
=RIGHT((text),2)&"/"&MID((text),5,2)&"/"&LEFT((text),4)
Use the DATEVALUE() function which takes as input a text format. Not that if your output cell is not in Date format you will see a simple int insteade of the expected 1/1/2016.
I refer you to the DATEVALUE function documentation for more insight on the input format and so on
This question already has answers here:
Converting Time Formats in Excel
(2 answers)
Closed 6 years ago.
May I know the way to convert "1 h 49 m 57 s" to 1:49:57 in Excel.
Thanks in advance
You can use this formula to replace letters to colons then convert text to value:
=VALUE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A5," h ",":")," m ",":")," s",""))
After inserting the formula set the cell's number format to "time".
I am having an issue printing a string in MATLAB (2012a) using the fprtinf command (and sprintf).
I have an array of 12 dates (numeric). I am converting them to strings using the following command:
months = datestr(data(:,1)-365,12); %Mar13 format
I obtain the following (and desired) output when I call the months variable:
Jan12
Feb12
Mar12
Apr12
etc..
The issue is when I call the fprintf or sprintf, say with the following code:
fprintf('%s', months(1))
I will only get the first letter of the month and not the full string. Any idea how to make it print the full string?
Thanks!
The resulting data type for your months variable is an NxM character array. You need to process it as a cell array of strings instead.
dates = num2cell(data(:,1)-365)
months = cellfun(#(x) datestr(x,12),dates,'UniformOutput',false)
fprintf('%s', months{1})
should get you what you want.
Simply change your call to
fprintf('%s', months(1, :))
datestr returns the string of each of the supplied dates on a separate row.
Alternatively you could use the cellstr function to convert the result to a cell array (this would also work with non fixed-length date formats like 'dddd')
months = cellstr(months);
fprintf('%s', months{1});
This question already has answers here:
Excel Date to String conversion
(10 answers)
Closed 5 years ago.
In Excel, I'm trying to have a cell look something like:
by no later than August 27, 2012
When I try to concatenate a string with a DATE, for example:
="by no later than " & DATE(YEAR(TODAY()),MONTH(TODAY()),DAY(TODAY()))
I get an output like this:
by no later than 41118
How can I get a date to show up instead of an integer?
DATE builds a date timestamp. You need to convert that to a string. See this question for how to do so:
Excel Date to String conversion
It would look something like this:
=TEXT(DATE(YEAR(TODAY()),MONTH(TODAY()),DAY(TODAY())), "DD/MM/YYYY hh:mm:ss")
You don't really need DATE function at all for today's date, you could use just
="by no later than "&TEXT(TODAY(),"mmmm d, yyyy")
If you format the cell containing your number (41118) as Date, you'll get exactly 27.8.2012. Simple!