François Michonneau <[log in to unmask]>

3:43 PM (4 minutes ago)
to UF
Hi Nihal

  Here is a possible solution

```
library(tidyverse)
library(readxl)

dt <- read_excel("example_for_R_listerv.xlsx", skip = 1) %>%
  mutate(date = format(time, "%Y-%m-%d"))
```

If your dates are already properly formatted, you don't have to use regular expressions. The functions that read data in the tidyverse detect automatically that it's a date and parse them as such. Once your data is in the date format, you can use the "format" function to extract the year (%Y), month (%m) and day (%d) in a new column here named "date". However, depending on what you are trying to accomplish with your data you may not have to do that. For instance, you can filter for a time period directly on your initial column without having to extract the exact day.

Also note that I put "skip = 1" and the column headers are actually not in your first row of the spreadsheet (which might be why the "time" column was not recognized in the code example you sent).

  I hope this helps.

  Best
  -- François


On Fri, Jul 27, 2018 at 3:31 PM El Rouby,Nihal M <[log in to unmask]> wrote:

Dear All-

 

Trying to extract dates from a variable in a data frame and then make a new column with these dates. I’m using regular expression along str_extract command from package stringr.

 

I’m getting the error  “Error in geno_time$time : $ operator is invalid for atomic vectors” when I used this command below on my dataframe.

 

geno_time<-str_extract(geno_time$time,"[0-9]+\\/[0-9]+\\/[0-9]+[:blank:]?")

 

I’m attaching example of my data and what I want to accomplish.

 

I appreciate your input and have a great weekend.

 

Best,

Nihal

This list strives to be beginner friendly. However, we still ask that you PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
This list strives to be beginner friendly. However, we still ask that you PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.