This question already has answers here:
integer data frame to date in R [duplicate]
(3 answers)
Closed 4 years ago.
My raw data file have the date columns with numbers like "180410". These are read as integers when I import the data into R. Now I tried converting them to dates using as.date but I got only N/A in these columns. Is there a way to covert them to actual dates? Please help
You need to check the format part of as.Date, it needs to match the input (2 digit year, and no spacing). If you have the wrong input you get NA. The following example produces NA because there are no hyphens in your input:
as.Date("180412", "%y-%m-%d")
Use this instead:
as.Date("180412", "%y%m%d")
Related
This question already has answers here:
dplyr: how to reference columns by column index rather than column name using mutate?
(6 answers)
The difference between bracket [ ] and double bracket [[ ]] for accessing the elements of a list or dataframe
(11 answers)
Closed 1 year ago.
I am trying to change a txt column that has a date in the format %m/%d/%Y to a date but the problem is that I cannot use its name because its not known (I am doing an app so the name can vary), one thing that I am sure is that is the column number 1 of the dataframe.
using the name of the column the R code would be
dates <- as.Date(data$date, "%m/%d/%Y")
But I cant do this, I need to use the index so I tried this
dates <- as.Date(data[1], "%m/%d/%Y")
But I get this error
Error in as.Date.default(data[1], "%m/%d/%Y") :
do not know how to convert 'data[1]' to class “Date”
thanks for the help
This question already has answers here:
How to convert a factor to integer\numeric without loss of information?
(12 answers)
as.numeric with comma decimal separators?
(7 answers)
Closed 3 years ago.
I´m sure this is simple, but I couldn´t find the right answer to my specific problem.
I have a data frame in R. The first column has a DateTime (POSIXct) format.
The values in all the other columns have the format "factor".
Since all of the values in these columns are numbers I want to convert them in to a numeric format.
An additional problem is that since I´m from europe the numbers (factors) have "," as a decimal mark.
How do I convert these columns in to a numeric format?
This question already has an answer here:
format year-month to POSIXct [duplicate]
(1 answer)
Closed 3 years ago.
I have two different dataframe with different date formats
date1<-c('2001-01-30', '2001-02-25')
data2 <- c('200101','200102')
I want to convert these dates in the same format so I can merge my two different dataframes by date.
The frequency is on a monthly basis.
Can someone help me with this task?
EDIT(Caution): This is not best practice and the use of standard parsers is recommended. However, different requirements may come up and therefore this answer simply shows how to get the expected format in the OP and in a comment from OP.
We could use:
paste0(substring(data2,nchar(data2)-1,nchar(data2)),"/",substring(data2,1,4))
#[1] "01/2001" "02/2001"
To get the reverse:
paste0(substring(data2,1,4),"-",substring(data2,nchar(data2)-1,nchar(data2)))
[1] "2001-01" "2001-02
This question already has answers here:
Converting date in Year.decimal form in R
(2 answers)
Closed 3 years ago.
I have one column(date) in numeric format. I want to extract date in the format of (day, month) from numeric numbers in the table. The table name in ABC and column name is Date.
Date(sample)
95.00000
95.08333
95.16667
95.25000
95.33333
95.41667
Q: Shall I first convert numeric numbers to date class and then extract date?
Can anyone help with this?
This should do the trick in base R :
format(as.Date(as.numeric(Sys.Date()), origin = "1970-01-01"), "(%d, %m)")
#(26, 02)
This question already has answers here:
How to convert Excel date format to proper date in R
(5 answers)
Closed 4 years ago.
I am new to R.
How to format a column which contains over 1000 records of dates(this is the format i am looking for 2016-01-05, 2016-04-12).
After importing the excel file in to R, above dates have changed to 16896 16903 and so on, what is the best way to format it.
library(lubridate)
date <- ymd("2016-01-05")
this will do the trick for you. change the date with you column name