0

Goodmorning everybody! I'm struggling a bit with R and the good book R IN ACTION: Data analysis and graphics with R. Searching the web hasn't resulted in any valuable effort, and neither reading the 'man' page for as.Date.

PROBLEM

I've this input in R: date <- c("10/24/08","10/28/08","10/1/08","10/12/08","5/1/09")

which then is used to create a data.frame named leadership as follow:

manager <- c(1,2,3,4,5)
country <- c("US","US","UK","UK","UK")
gender <- c("M","F","F","M","F")
age <- c(32,45,25,39,99)
q1 <- c(5,3,3,3,2)
q2 <- c(4,5,5,3,2)
q3 <- c(5,2,5,4,1)
q4 <- c(5,5,5,NA,2)
q5 <- c(5,5,2,NA,1)
leadership <- data.frame(manager, date, country, gender, age, q1,q2,q3,q4,q5, stringsAsFactors=F)

If i print it out, what I get is:

  manager     date country gender age q1 q2 q3 q4 q5
1       1 10/24/08      US      M  32  5  4  5  5  5
2       2 10/28/08      US      F  45  3  5  2  5  5
3       3  10/1/08      UK      F  25  3  5  5  5  2
4       4 10/12/08      UK      M  39  3  3  4 NA NA
5       5   5/1/09      UK      F  99  2  2  1  2  1

and

str(leadership)
'data.frame':   5 obs. of  10 variables:
 $ manager: num  1 2 3 4 5
 $ date   : chr  "10/24/08" "10/28/08" "10/1/08" "10/12/08" ...
 $ country: chr  "US" "US" "UK" "UK" ...
 $ gender : chr  "M" "F" "F" "M" ...
 $ age    : num  32 45 25 39 99
 $ q1     : num  5 3 3 3 2
 $ q2     : num  4 5 5 3 2
 $ q3     : num  5 2 5 4 1
 $ q4     : num  5 5 5 NA 2
 $ q5     : num  5 5 2 NA 1

I need then to change the variable date format

leadership$date <- as.Date(leadership$date, format="%m/%d/%y")
leadership
  manager       date country gender age q1 q2 q3 q4 q5
1       1 2008-10-24      US      M  32  5  4  5  5  5
2       2 2008-10-28      US      F  45  3  5  2  5  5
3       3 2008-10-01      UK      F  25  3  5  5  5  2
4       4 2008-10-12      UK      M  39  3  3  4 NA NA
5       5 2009-05-01      UK      F  99  2  2  1  2  1

Now I'd like, is the date column of my data.frame to be displayed in the italian format: day/month/year, such as (the first observation) 24/10/08. So I issue the following command:

format(leadership$date, "%d/%m/%y")
[1] "24/10/08" "28/10/08" "01/10/08" "12/10/08" "01/05/09"

That makes it as desired. But if I print the data.frame again, what I get is not what I want, even though the data format should have been correctly set:

 leadership
  manager       date country gender age q1 q2 q3 q4 q5
1       1 2008-10-24      US      M  32  5  4  5  5  5
2       2 2008-10-28      US      F  45  3  5  2  5  5
3       3 2008-10-01      UK      F  25  3  5  5  5  2
4       4 2008-10-12      UK      M  39  3  3  4 NA NA
5       5 2009-05-01      UK      F  99  2  2  1  2  1

 str(leadership)
'data.frame':   5 obs. of  10 variables:
 $ manager: num  1 2 3 4 5
 $ date   : Date, format: "2008-10-24" "2008-10-28" ...
 $ country: chr  "US" "US" "UK" "UK" ...
 $ gender : chr  "M" "F" "F" "M" ...
 $ age    : num  32 45 25 39 99
 $ q1     : num  5 3 3 3 2
 $ q2     : num  4 5 5 3 2
 $ q3     : num  5 2 5 4 1
 $ q4     : num  5 5 5 NA 2
 $ q5     : num  5 5 2 NA 1

I really don't know how to set it correctly. My locale is:

Sys.getlocale()
[1] "LC_CTYPE=en_US.UTF-8;LC_NUMERIC=C;LC_TIME=it_IT.UTF-8;LC_COLLATE=en_US.UTF-8;LC_MONETARY=it_IT.UTF-8;LC_MESSAGES=en_US.UTF-8;LC_PAPER=it_IT.UTF-8;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=it_IT.UTF-8;LC_IDENTIFICATION=C")

Time format here is already in the 'italian-style'..

For reference, I'm running Ubuntu 16.04.2 LTS and R version 3.3.3 (2017-03-06) -- "Another Canoe" Platform: x86_64-pc-linux-gnu (64-bit)

Hence: how can I modify the date column to be displayed as I'd like?

Thanks in advance and sorry for me being so long-winded! Cheers! =-P

Marc
  • 3
  • 1

1 Answers1

0

You don't assign your calculated data back to your data frame, that's why it dosen't change. Try this:

leadership$date <- format(leadership$date, "%d/%m/%y")
FlorianGD
  • 2,336
  • 1
  • 15
  • 32
  • Ouch! I feel sad.. Hours spent to search for a solution.. When it was so easy.. I feel such a blithering idiot.. Thanks for replying! – Marc Apr 11 '17 at 14:21