I was playing around with a data frame and I can't wrap my head around a problem. Here is the code I used:
Died.At <- c(22,40,72,41)
Writer.At <- c(16, 18, 36, 36)
First.Name <- c("John", "Edgar", "Walt", "Jane")
Second.Name <- c("Doe", "Poe", "Whitman", "Austen")
Sex <- c("MALE", "MALE", "MALE", "FEMALE")
Date.Of.Death <- c("2015-05-10", "1849-10-07", "1892-03-26","1817-07-18")
writersdataframe <- data.frame(Died.At, Writer.At, I(First.Name), I(Second.Name), Sex, as.Date(Date.Of.Death))
This is the result:
str (writersdataframe)
'data.frame': 4 obs. of 6 variables:
$ Died.At : num 22 40 72 41
$ Writer.At : num 16 18 36 36
$ First.Name : 'AsIs' chr "John" "Edgar" "Walt" "Jane"
$ Second.Name : 'AsIs' chr "Doe" "Poe" "Whitman" "Austen"
$ Sex : Factor w/ 2 levels "FEMALE","MALE": 2 2 2 1
$ as.Date.Date.Of.Death.: Date, format: "2015-05-10" "1849-10-07" "1892-03-26" ...
I wrote the code like this because I want R to interpret Date.Of.Death as a date, but I do not want as.Date to show in the name of the column inside the data frame. I found a way to do it, which is is change the format before creating the data frame:
Date.Of.Death <- as.Date(Date.Of.Death)
writersdataframe <- data.frame(Died.At, Writer.At, I(First.Name), I(Second.Name), Sex, I(Date.Of.Death))
I checked with:
class(writersdataframe$Date.Of.Death)
[1] "AsIs" "Date"
What I was wondering is if I can create the data frame while treating Date.Of.Death as.Date directly in the function data.frame. Is there a reason that doing it (e.g.:
writersdataframe <- data.frame(Died.At, Writer.At, I(First.Name), I(Second.Name), Sex, as.Date(Date.Of.Death))
) renames the column title or did I make a mistake?