Hi I was wondering if someone could help me figure out how to convert a column in a data frame that is a factor into a Date (%y/%m/%d)
str(full_data_1)
'data.frame': 36 obs. of 5 variables:
$ id : chr "123A" "123A" "123A" "123A" ...
$ Date : Date, format: NA NA ...
$ usage : num 10 12 40 20 11 20 30 20 16 15 ...
$ treatment: num 0 0 0 0 0 0 0 0 0 0 ...
$ Gender : Factor w/ 2 levels "Female","Male": 2 2 2 2 2 2 2 2 2 2 ...
Currently This is what I am using and I keep getting NA when doing so.
full_data_1$Date
<-as.Date(as.character(full_data_1$Date
),format = "%m/%d/%Y")
FJCC
July 24, 2020, 5:46pm
2
Your method works from me.
Can you post the output of
dput(head(full_data_1))
Here is the test code I used to replicate your method.
DF <- data.frame(Date=c("9/3/1976","9/4/1976", "9/5/1976"))
str(DF)
#> 'data.frame': 3 obs. of 1 variable:
#> $ Date: Factor w/ 3 levels "9/3/1976","9/4/1976",..: 1 2 3
DF$Date <- as.Date(as.character(DF$Date), format = "%m/%d/%Y")
str(DF)
#> 'data.frame': 3 obs. of 1 variable:
#> $ Date: Date, format: "1976-09-03" "1976-09-04" ...
Created on 2020-07-24 by the reprex package (v0.2.1)
This is the result I get:
> dput(head(full_data_1))
structure(list(id = c("123A", "123A", "123A", "123A", "123A",
"123A"), Date = structure(c(NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_), class = "Date"), usage = c(10, 12, 40, 20,
11, 20), treatment = c(0, 0, 0, 0, 0, 0), Gender = structure(c(2L,
2L, 2L, 2L, 2L, 2L), .Label = c("Female", "Male"), class = "factor")), row.names = c(NA,
6L), class = "data.frame")
Hermes
July 24, 2020, 6:27pm
4
library(data.table)
> DF <- data.table(Date=c("9/3/1976","9/4/1976", "9/5/1976"),stringsAsFactors = TRUE)
> str(DF)
Classes ‘data.table’ and 'data.frame': 3 obs. of 1 variable:
$ Date: Factor w/ 3 levels "9/3/1976","9/4/1976",..: 1 2 3
- attr(*, ".internal.selfref")=<externalptr>
> DF[,Date :=as.Date(as.character(DF$Date), format = "%m/%d/%Y"),]
> str(DF)
Classes ‘data.table’ and 'data.frame': 3 obs. of 1 variable:
$ Date: Date, format: "1976-09-03" "1976-09-04" "1976-09-05"
- attr(*, ".internal.selfref")=<externalptr>
FJCC
July 24, 2020, 6:35pm
5
@MaiCabrera - Is that the result from full_data_1 before you try the date conversion? The Date column is already NA. Please post the result using the data before you attempt the conversion.
What you saw was actually the result after I ran the date conversion. Hence why I am stumped as to why it still shows as NA
FJCC
July 24, 2020, 7:06pm
7
I cannot try to debug the problem unless I see the data before you attempt the conversion.
@FJCC my bad! Thank you for the clarification! this is what the df is before the date conversion.
str(full_data_1)
'data.frame': 36 obs. of 5 variables:
$ id : chr "123A" "123A" "123A" "123A" ...
$ Date : Factor w/ 12 levels "2018-01-01","2018-02-01",..: 1 10 6 2 11 7 3 12 8 4 ...
$ usage : num 10 12 40 20 11 20 30 20 16 15 ...
$ treatment: num 0 0 0 0 0 0 0 0 0 0 ...
$ Gender : Factor w/ 2 levels "Female","Male": 2 2 2 2 2 2 2 2 2 2 ...
and this is after the conversion
str(full_data_1)
'data.frame': 36 obs. of 5 variables:
$ id : chr "123A" "123A" "123A" "123A" ...
$ Date : Date, format: NA NA ...
$ usage : num 10 12 40 20 11 20 30 20 16 15 ...
$ treatment: num 0 0 0 0 0 0 0 0 0 0 ...
$ Gender : Factor w/ 2 levels "Female","Male": 2 2 2 2 2 2 2 2 2 2 ...
FJCC
July 24, 2020, 8:36pm
9
The format of your date in the raw file is %Y-%m-%d. Try
DF$Date <- as.Date(as.character(DF$Date), format = "%Y-%m-%d")
In fact, the format argument is not even necessary in this case but I would probably use it just for clarity.
1 Like
system
Closed
July 31, 2020, 8:48pm
11
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.