0

I am new to R, I have a big data set with many columns. I have to convert some columns to date format but do not want do it one by one .I want to write a function that giving data set name and column name and it change the column format .

chr_to_date <-function(data_name,Column_name){
  data_name$Column_name <-as.POSIXlt.date(data_name$Column_name)
  return(data_name$Column_name)
}
chr_to_date(data_name=SampleSet,Column_name=LatestSend)

when I run it ,it gives the below error

Error in as.POSIXct.default(x) : do not know how to convert 'x' to class “POSIXct”

Any help you can provide will be greatly appreciated.

Benjamin
  • 16,897
  • 6
  • 45
  • 65
Azi
  • 1
  • 1
  • Possible duplicate of [Converting multiple columns in an R dataframe to Date Format](https://stackoverflow.com/questions/22772668/converting-multiple-columns-in-an-r-dataframe-to-date-format) – Benjamin Mar 28 '18 at 10:59
  • The question I've linked to should help you write an `lapply` call that can change all of the columns to date classes in one pass. Please note that it is not necessary to us `as.POSIXlt.date`; you may simply use `as.POSIXlt`, or preferably, `as.POSIXct` and R will sort out the correct method to use on your data. – Benjamin Mar 28 '18 at 11:01
  • Thanks for link but my columns name are very different ,cant use grep to find match .My columns name are declared in a vector so I want o pass the column name to function ,then function convert the format. – Azi Mar 28 '18 at 11:41
  • Remember that in R we can think dataframe columns as array, separate them and work with individual functions. – Arduin Mar 28 '18 at 11:50
  • Possible duplicate of [Pass a data.frame column name to a function](https://stackoverflow.com/questions/2641653/pass-a-data-frame-column-name-to-a-function) – Rui Barradas Mar 28 '18 at 18:26

1 Answers1

0

There are many ways how you work out with dates in R.

For exemple,

suppose you have a date frame as follow

##creating a data frame
dates <- c("2018/01/25","2017/04/14","2012/12/24","2018/05/15","2058/02/25")
index <- 1:5

df <- data.frame(dates=dates, index=index, stringsAsFactors = F)

Checking the struct of the date we obtain

##checking the type of the dates
str(df)
'data.frame':   5 obs. of  2 variables:
 $ dates: chr  "2018/01/25" "2017/04/14" "2012/12/24" "2018/05/15" ...
 $ index: int  1 2 3 4 5

So we want convert the character column of our data frame (df) to a date format

##converting data type in R

df$dates <- as.Date(df$dates, "%Y/%m/%d")

#checking the new struct of the data frame
str(df)

'data.frame':   5 obs. of  2 variables:
 $ dates: Date, format: "2018-01-25" "2017-04-14" "2012-12-24" "2018-05-15" ...
 $ index: int  1 2 3 4 5

You could also use a package called lubridate that gives you more date options functions to work with.

For exemple

##other ways to work out
library(lubridate)

df$dates <- ymd(df$dates)

str(df)

data.frame':    5 obs. of  2 variables:
 $ dates: Date, format: "2018-01-25" "2017-04-14" "2012-12-24" "2018-05-15" ...
 $ index: int  1 2 3 4 5

Nevertheless, when we used those functions by default they changing the slash by hifen. We can change that using format function

format(df$dates, "%Y/%m/%y")
"2018/01/18" "2017/04/17" "2012/12/12" "2018/05/18" "2058/02/58"

or whenever symbol we preferer

format(df$dates, "%Y&%m&%y")
"2018&01&18" "2017&04&17" "2012&12&12" "2018&05&18" "2058&02&58"
Arduin
  • 233
  • 4
  • 15
  • Thanks for your comment .I solved it this way. while(counter < length(DateColumn)) { SampleSet[DateColumn[counter]]<-lapply((SampleSet[DateColumn[counter]]),as.POSIXct) counter=counter+1 } – Azi Mar 28 '18 at 16:06