简体   繁体   中英

Date format conversion in R

I was using the syntax

df$new_column_name <- format(as.Date(df$original_column_name, format = "%d/%m/%Y"),"%m/%d/%Y")

to convert date format of a column in a dataframe called Dailyactivity_Records. The original date format for the column ActvityDate is mm/dd/YYYY in chr format. as can be seen in the console.

> dailyactivity_Records <- read.csv("dailyActivity_calories_intensities_steps.csv")
> str(dailyactivity_Records)
'data.frame':   940 obs. of  15 variables:
 $ Id                      : num  1.5e+09 1.5e+09 1.5e+09 1.5e+09 1.5e+09 ...
 $ ActivityDate            : chr  "04/12/2016" "4/13/2016" "4/14/2016" "4/15/2016" ...
 $ TotalSteps              : int  13162 10735 10460 9762 12669 9705 13019 15506 10544 9819 ...
 $ TotalDistance           : num  8.5 6.97 6.74 6.28 8.16 6.48 8.59 9.88 6.68 6.34 ...
 $ TrackerDistance         : num  8.5 6.97 6.74 6.28 8.16 6.48 8.59 9.88 6.68 6.34 ...
 $ LoggedActivitiesDistance: num  0 0 0 0 0 0 0 0 0 0 ...
 $ VeryActiveDistance      : num  1.88 1.57 2.44 2.14 2.71 3.19 3.25 3.53 1.96 1.34 ...
 $ ModeratelyActiveDistance: num  0.55 0.69 0.4 1.26 0.41 0.78 0.64 1.32 0.48 0.35 ...
 $ LightActiveDistance     : num  6.06 4.71 3.91 2.83 5.04 2.51 4.71 5.03 4.24 4.65 ...
 $ SedentaryActiveDistance : num  0 0 0 0 0 0 0 0 0 0 ...
 $ VeryActiveMinutes       : int  25 21 30 29 36 38 42 50 28 19 ...
 $ FairlyActiveMinutes     : int  13 19 11 34 10 20 16 31 12 8 ...
 $ LightlyActiveMinutes    : int  328 217 181 209 221 164 233 264 205 211 ...
 $ SedentaryMinutes        : int  728 776 1218 726 773 539 1149 775 818 838 ...
 $ Calories                : int  1985 1797 1776 1745 1863 1728 1921 2035 1786 1775 ...

The required converted date format for the column ActvityDate specified in the last line of code is "%d/%m" where as the converted date format is %Y/%m/%d (which I believe is the default date format of date in R). Can someone clarify why?

Please see the console below:

> ## converting column ID to character and ACtivityDate to date format
> dailyactivity_Records$Id <- as.character(dailyactivity_Records$Id)
> dailyactivity_Records$Date_ddmm = as.Date(dailyactivity_Records$ActivityDate, format = "%m/%d/%Y", "%d/%m")
> str(dailyactivity_Records)

'data.frame':   940 obs. of  16 variables:
 $ Id                      : chr  "1503960366" "1503960366" "1503960366" "1503960366" ...
 $ ActivityDate            : chr  "04/12/2016" "4/13/2016" "4/14/2016" "4/15/2016" ...
 $ TotalSteps              : int  13162 10735 10460 9762 12669 9705 13019 15506 10544 9819 ...
 $ TotalDistance           : num  8.5 6.97 6.74 6.28 8.16 6.48 8.59 9.88 6.68 6.34 ...
 $ TrackerDistance         : num  8.5 6.97 6.74 6.28 8.16 6.48 8.59 9.88 6.68 6.34 ...
 $ LoggedActivitiesDistance: num  0 0 0 0 0 0 0 0 0 0 ...
 $ VeryActiveDistance      : num  1.88 1.57 2.44 2.14 2.71 3.19 3.25 3.53 1.96 1.34 ...
 $ ModeratelyActiveDistance: num  0.55 0.69 0.4 1.26 0.41 0.78 0.64 1.32 0.48 0.35 ...
 $ LightActiveDistance     : num  6.06 4.71 3.91 2.83 5.04 2.51 4.71 5.03 4.24 4.65 ...
 $ SedentaryActiveDistance : num  0 0 0 0 0 0 0 0 0 0 ...
 $ VeryActiveMinutes       : int  25 21 30 29 36 38 42 50 28 19 ...
 $ FairlyActiveMinutes     : int  13 19 11 34 10 20 16 31 12 8 ...
 $ LightlyActiveMinutes    : int  328 217 181 209 221 164 233 264 205 211 ...
 $ SedentaryMinutes        : int  728 776 1218 726 773 539 1149 775 818 838 ...
 $ Calories                : int  1985 1797 1776 1745 1863 1728 1921 2035 1786 1775 ...
 $ Date_ddmm               : Date, format: "2016-04-12" "2016-04-13" "2016-04-14" "2016-04-15" ...

First, if you read ?as.Date (strongly encouraged), you'll see that your third argument (unnamed) is being interpreted as tryFormats = "%d/%m" . However, since

tryFormats: 'character' vector of 'format' strings to try if 'format'
          is not specified.

and you do include formats= , then it is doing nothing.

Second, what you are trying to do should be done in two steps: first convert to a Date , then convert it from a number-like object to a string as you want. From here, btw, your dates are no longer dates, they will no longer be something on which you can do date-math (eg, add/substract/difference).

vec <- c("04/12/2016", "4/13/2016", "4/14/2016", "4/15/2016")
as.Date(vec, format = "%m/%d/%Y")
# [1] "2016-04-12" "2016-04-13" "2016-04-14" "2016-04-15"
as.Date(vec, format = "%m/%d/%Y") + 5
# [1] "2016-04-17" "2016-04-18" "2016-04-19" "2016-04-20"
format(as.Date(vec, format = "%m/%d/%Y"), "%d/%m")
# [1] "12/04" "13/04" "14/04" "15/04"
format(as.Date(vec, format = "%m/%d/%Y"), "%d/%m") + 5
# Error in format(as.Date(vec, format = "%m/%d/%Y"), "%d/%m") + 5 : 
#   non-numeric argument to binary operator

If you need number-like operations on it (including ranges), you must keep it as a Date . If that's the case, I suggest you consider that you can keep it as a date for all of your processing, and then only when you render your data for visualization (plots, tables, etc), then and only then do you need the string representation of %d/%m . You can add that as another column in addition to the "real" Date object, perhaps

dailyactivity_Records$ActivityDate <- as.Date(dailyactivity_Records$ActivityDate, format = "%m/%d/%Y")
dailyactivity_Records$Date_ddmm <- format(dailyactivity_Records$ActivityDate , "%d/%m")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM