Convert columns to posixct using sapply and keeping datetime format in R

Gaurav Bansal

I want to use sapply (or something similar) to convert certain columns to POSIXct in an R data.frame but maintain the datetime format of the columns. When I do it currently, it converts the format to numeric. How can I do this? An example is below.

#sample dataframe
df <- data.frame(
  var1=c(5, 2),
  char1=c('he', 'she'),
  timestamp1=c('2019-01-01 20:30:08', '2019-01-02 08:27:34'),
  timestamp2=c('2019-01-01 12:24:54', '2019-01-02 10:57:47'),
  stringsAsFactors = F
)

#Convert only columns with 'timestamp' in name to POSIXct
df[grep('timestamp', names(df))] <- sapply(df[grep('timestamp', names(df))], function(x) as.POSIXct(x, format='%Y-%m-%d %H:%M:%S'))
df
  var1 char1 timestamp1 timestamp2
1    5    he 1546392608 1546363494
2    2   she 1546435654 1546444667

Note: I can use as.posixlt instead of as.posixctand it works, but I want the data in POSIXct format. I also tried converting to POSIXlt first and then to POSIXct, but that also ended up converting the columns to numeric.

MrFlick

Use lapply rather than sapply. The "s" in sapply is for simplify and it turns the result into a matrix but sapply can't create a matrix of POSIXct values so it gets cast to a simple numeric matrix. But if you keep it a list, you don't lose the class.

df[grep('timestamp', names(df))] <- lapply(df[grep('timestamp', names(df))], function(x) as.POSIXct(x, format='%Y-%m-%d %H:%M:%S'))

You could also do this fairly easily with dplyr

library(dplyr)
df %>% mutate_at(vars(contains("timestamp")), as.POSIXct)

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to clean columns & convert to datetime using python

分類Dev

using POSIXct in R

分類Dev

SQL DateTime Format Convert

分類Dev

R apply()関数format()over POSIXct

分類Dev

Using pd.to_datetime to convert int with format HHMMSS

分類Dev

convert datetime to float in R

分類Dev

PHP: Convert epoch to MySQL DateTime format

分類Dev

How to convert a datetime format to minutes - pandas

分類Dev

How to convert string data with variable format to datetime?

分類Dev

How to convert a column with null values to datetime format?

分類Dev

Convert datetime from Excel to R

分類Dev

Convert a specific format dateTime string to DateTime in C#

分類Dev

Merge three dataframes by two columns, keeping only the largest value in R

分類Dev

Using pandas groupby but keeping the order of other columns, then shift within group

分類Dev

sapplyを使用し、Rで日時形式を維持して、列をposixctに変換します

分類Dev

Convert factors into multiple columns in R

分類Dev

Rails DateTime re-format using #{DateTime.now}

分類Dev

Convert factor containing AM/PM to 24 hours HH:MM time format using R programming

分類Dev

How to convert to datetime if the format of dates changes gradually through the column?

分類Dev

How to format columns so they line using ruby

分類Dev

R crop raster using polygon keeping cells along the border

分類Dev

Returning row number using sapply

分類Dev

Convert date to character in particular format In R

分類Dev

Convert Rows into Columns by matching string in R

分類Dev

Using dplyr::if_else() in R to change the time zone of POSIXct timestamps based on value of another variable

分類Dev

Pyspark - Ranking columns keeping ties

分類Dev

How do I convert multiple `string` columns in my dataframe to datetime columns?

分類Dev

Using boost parse datetime string: With single digit hour format

分類Dev

Incorrect results using Pyspark-sql to convert unix time to datetime

Related 関連記事

  1. 1

    How to clean columns & convert to datetime using python

  2. 2

    using POSIXct in R

  3. 3

    SQL DateTime Format Convert

  4. 4

    R apply()関数format()over POSIXct

  5. 5

    Using pd.to_datetime to convert int with format HHMMSS

  6. 6

    convert datetime to float in R

  7. 7

    PHP: Convert epoch to MySQL DateTime format

  8. 8

    How to convert a datetime format to minutes - pandas

  9. 9

    How to convert string data with variable format to datetime?

  10. 10

    How to convert a column with null values to datetime format?

  11. 11

    Convert datetime from Excel to R

  12. 12

    Convert a specific format dateTime string to DateTime in C#

  13. 13

    Merge three dataframes by two columns, keeping only the largest value in R

  14. 14

    Using pandas groupby but keeping the order of other columns, then shift within group

  15. 15

    sapplyを使用し、Rで日時形式を維持して、列をposixctに変換します

  16. 16

    Convert factors into multiple columns in R

  17. 17

    Rails DateTime re-format using #{DateTime.now}

  18. 18

    Convert factor containing AM/PM to 24 hours HH:MM time format using R programming

  19. 19

    How to convert to datetime if the format of dates changes gradually through the column?

  20. 20

    How to format columns so they line using ruby

  21. 21

    R crop raster using polygon keeping cells along the border

  22. 22

    Returning row number using sapply

  23. 23

    Convert date to character in particular format In R

  24. 24

    Convert Rows into Columns by matching string in R

  25. 25

    Using dplyr::if_else() in R to change the time zone of POSIXct timestamps based on value of another variable

  26. 26

    Pyspark - Ranking columns keeping ties

  27. 27

    How do I convert multiple `string` columns in my dataframe to datetime columns?

  28. 28

    Using boost parse datetime string: With single digit hour format

  29. 29

    Incorrect results using Pyspark-sql to convert unix time to datetime

ホットタグ

アーカイブ