1

I am trying to change a txt column that has a date in the format %m/%d/%Y to a date but the problem is that I cannot use its name because its not known (I am doing an app so the name can vary), one thing that I am sure is that is the column number 1 of the dataframe.

using the name of the column the R code would be

dates <- as.Date(data$date, "%m/%d/%Y")

But I cant do this, I need to use the index so I tried this

dates <- as.Date(data[1], "%m/%d/%Y")

But I get this error

Error in as.Date.default(data[1], "%m/%d/%Y") : 
  do not know how to convert 'data[1]' to class “Date”

thanks for the help

Juan Lozano
  • 635
  • 1
  • 6
  • 17
  • 2
    `data[[1]]` - you need to grab the vector not the list. `[[` is analogous to `$` – thelatemail Jul 16 '21 at 01:45
  • `data[, 1]` will also work. Look at `str(data[1]`, `str(data[[1]]`, and `str(data[ , 1]`. The first one returns a data frame, the second and third ones a vector. – dcarlson Jul 16 '21 at 01:50
  • @dcarlson - unless you're working with a tibble, then you get a tibble back with the `data[,1]` version. – thelatemail Jul 16 '21 at 01:58

0 Answers0