Convert Factor to Date Class for Multiple Columns

Convert factor to date class for multiple columns

Given the format of your dates, you may try this:

# sample data
df <- data.frame(a = 1:2,
d1 = factor(c("2013/01/01", "2014/01/01")),
d2 = factor(c("2013/01/01", "2014/01/01")),
b = 3:4)

df[ , 2:3] <- lapply(df[ , 2:3], as.Date)

str(df)
# 'data.frame': 2 obs. of 4 variables:
# $ a : int 1 2
# $ d1: Date, format: "2013-01-01" "2014-01-01"
# $ d2: Date, format: "2013-01-01" "2014-01-01"
# $ b : int 3 4

Select multiple date columns and convert factor variables to date

Updated answer based on new data provided.

Try the following. There's no need to strip out the time component of the date-time string. You can parse it using the lubridate function which matches the data (in this case, dmy_hm()) then disregard it.

dfq_parsed <- dfq %>%
mutate(across(contains("date", ignore.case = TRUE), dmy_hm))

This yields:

         ID Date_Construct   Date_use  Comp
1 0001 2018-03-10 2007-08-02 Revis
2 34560 2015-03-21 2007-10-31 Succ
3 100041531 2012-02-20 2008-08-13 Revis

Where the dates are as POSIXct, but that's easy enough to work with:

'data.frame':   3 obs. of  4 variables:
$ ID : chr "0001" "34560" "100041531"
$ Date_Construct: POSIXct, format: "2018-03-10" "2015-03-21" "2012-02-20"
$ Date_use : POSIXct, format: "2007-08-02" "2007-10-31" "2008-08-13"
$ Comp : chr "Revis" "Succ" "Revis"

Convert multiple columns of numeric data to dates in R

You may need a function that tells R that these integers represent dates, and then you need to apply that function to each column of your dataframe:

myfun <- function(x) as.Date(x, format="%Y-%m-%d", origin="1899-12-30")
xlnew <- data.frame(lapply(xl, myfun))

You can avoid all this by passing function anonymously as well or like one of the answers. Also, options(stringsAsFactors = FALSE) is also set in my environment for unwanted changes to factors.

Logic:

Excel date starts from 1900-01-01 with the index of 1, However R date usually we consider origin at 1970-01-01. There is a difference of 70 years, plus 1 day difference of indexing as R first date starts with the index of 0 not 1.Also, there is a bug in excel due to its historical reason where Excel considers 29-Feb-1900 a valid date, which is not true. Hence we should subtract 2 days ( 1 day difference due to indexing other 1 day due to the bug in excel) from the actual origin (which is 1900-01-01 of excel) to get the correct date.

Output of first 5 rows:

> xlnew
D D1 D2 D3
1 1991-09-02 2011-12-09 2011-06-01 2011-10-01
2 1952-08-07 2011-12-13 1985-06-07 2011-10-01
3 1951-11-15 2012-02-05 1993-06-15 2012-01-05
4 1969-03-04 2012-02-08 2010-08-01 2012-02-01
5 1939-12-02 2012-02-02 2009-07-01 2012-02-01

how to convert factor column into date in data frame in r

as.Date('02-JAN-18', format = '%d-%B-%y')

[1] "2018-01-02"

Convert multiple character columns to as.Date and time in R

You just need to tweak the arguments of mutate_at. Any additional arguments to as.Date are specified as arguments to mutate_at.

df2 <- df %>% mutate_at(vars(enter,disposal,date), as.Date, format="%Y-%m-%d")

The second part of your question has a similar solution.

df2 <- df2 %>% mutate_at(vars(clock, rolex), function(x) chron(times. = x))

Quarterly Data - Convert Factor to Date

Apply as.Date to the Date column, not the entire data.frame. No packages are used.

transform(df, Date = as.Date(Date))

giving:

        Date
1 2008-07-31
2 2008-07-31
3 2008-07-31
4 2008-07-31
5 2008-07-31
6 2008-07-31

Using R to convert factor to date

I think you need to supply a day value for the conversion to work:

cd$dates <- as.Date(
paste0(as.character(cd$StartDate), "-01"),
format = "%b-%y-%d")
##
R> str(cd)
#'data.frame': 3 obs. of 4 variables:
#$ StartDate: Factor w/ 3 levels "Dec-89","Jul-89",..: 1 2 3
#$ Phase : chr "Phase_2" "Phase_2" "Phase_1"
#$ Cancer : chr "breast" "breast" "breast"
#$ dates : Date, format: "1989-12-01" "1989-07-01" "1992-09-01"

Data:

cd <- read.table(
text = " StartDate Phase Cancer
1 Dec-89 Phase_2 breast
2 Jul-89 Phase_2 breast
3 Sep-92 Phase_1 breast",
header = TRUE,
stringsAsFactors = FALSE)
##
cd$StartDate <- as.factor(cd$StartDate)

Change the class from factor to numeric of many columns in a data frame

Further to Ramnath's answer, the behaviour you are experiencing is that due to as.numeric(x) returning the internal, numeric representation of the factor x at the R level. If you want to preserve the numbers that are the levels of the factor (rather than their internal representation), you need to convert to character via as.character() first as per Ramnath's example.

Your for loop is just as reasonable as an apply call and might be slightly more readable as to what the intention of the code is. Just change this line:

stats[,i] <- as.numeric(stats[,i])

to read

stats[,i] <- as.numeric(as.character(stats[,i]))

This is FAQ 7.10 in the R FAQ.

HTH



Related Topics



Leave a reply



Submit