-3

I am trying to convert the variable below from a factor with 542 levels to a Date which is recognised by R as numeric. I am very new to R and was unable to follow the explanations in other threads.

Thanks a lot! Sara

screen shot of data

Sara Correia
  • 45
  • 1
  • 6
  • 1
    share data please instead of sharing the screenshot – mtoto Jan 15 '16 at 12:10
  • 1
    This maybe helps: http://stackoverflow.com/questions/18160732/factor-as-date-to-numeric – Jaap Jan 15 '16 at 12:16
  • 2
    Furthermore: Please read the info about [how to ask a good question](http://stackoverflow.com/help/how-to-ask) and how to give a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610). This will make it much easier for others to help you. – Jaap Jan 15 '16 at 12:16
  • Thanks for your suggestions, I shall take that on board in the future. – Sara Correia Jan 18 '16 at 14:49

2 Answers2

0

Assuming you're after ISO8601 YYYYMMDD formated numbers, how about this?

dates <- c("18/08/2015","19/08/2015")
dateNumbers <- as.numeric(as.character(as.Date(dates, format = "%d/%m/%Y"), format="%Y%m%d"))
dateNumbers
[1] 20150818 20150819
Liesel
  • 2,929
  • 2
  • 12
  • 18
  • Thank you for this, I think it might! I have a further issue which is that for some reason my dates are in two different formats as follows: > data1 <- c("21/01/2015","22-01-2015") > dateNumbers <- as.numeric(as.character(as.Date(data1, format = "%d/%m/%Y"), format="%Y%m%d")) > dateNumbers [1] 20150121 NA – Sara Correia Jan 18 '16 at 13:24
  • It appears it is not possible to do paragraphs in the comments boxes, sorry! – Sara Correia Jan 18 '16 at 13:28
  • I have finally managed to tidy my data, thanks again for your help. – Sara Correia Jan 18 '16 at 14:49
-1

Your screenshot looks like a vector. So try this:

lapply(some_vec, function(date) gsub('-', '', date) )

EDIT: May be you need to reformat the date with:

some_vec <- as.Date(some_vec, format = '%Y-%m-%d')

before.

EDIT2:

First the code:

some_vec <- rep(factor(as.Date('18/08/2015', format = '%d/%m/%Y')), 30)
some_vec <- as.Date(some_vec, format = '%Y-%m-%d')
res <- unlist(lapply(some_vec, function(date) { 
   gsub('-', '', date)
}))
res
[1] "20150818" "20150818" "20150818" "20150818" "20150818" "20150818" "20150818" "20150818" "20150818" "20150818" "20150818" "20150818" "20150818" "20150818"
[15] "20150818" "20150818" "20150818" "20150818" "20150818" "20150818" "20150818" "20150818" "20150818" "20150818" "20150818" "20150818" "20150818" "20150818"
[29] "20150818" "20150818"

I tried to help blindfolded here. So, please don't be harsh. What I tried is to reformat the date (if even necessary). On my System R shows the date already in the 'YYYY-MM-DD' format (before the reformating).

some_vec
[1] 2015-08-18 2015-08-18 2015-08-18 2015-08-18 2015-08-18 2015-08-18 ...
[29] 2015-08-18 2015-08-18
Levels: 2015-08-18

Once the data has this format you can lapply over the list and kill the '-' signs.

Robert Kirsten
  • 474
  • 2
  • 5
  • 12
  • 1
    What is the first comamnd supposed to achieve? Your format string doesn't correspond to the date format in the data. – Roland Jan 15 '16 at 12:50
  • Start with `some_vec <- rep(factor('18/08/2015'), 30)` to reproduce OP's problem. `gsub` is not needed here. They probably want to have `Date`s. If they really want `numeric`s, they probably simply need to use `as.numeric` on the `Date`s. You shouldn't answer such unclear questions. – Roland Jan 15 '16 at 13:21
  • I won't in future! `as.numeric` wouldn't work here! Not for the factors nor the dates. – Robert Kirsten Jan 15 '16 at 13:27
  • `as.numeric` can of course turn `Date`s into `numeric`s: `as.numeric(as.Date("2016-01-15"))` – Roland Jan 15 '16 at 13:35
  • I know it can! I meant the reasonability to do so in this particular case. – Robert Kirsten Jan 15 '16 at 13:40