Newbie to RStudio - help with date import and transform

Hi all. I am not a programmer, but through a stepwise approach have learned basic functions of RStudio from readings in the community and Google. My goal is to make a PDF report containing many plots for my large data sets. I currently do this in excel which is painfully slow and ultimately I want to analyze and update my data in realtime (every day) over the 3-4 week study period - this allows us to recognize trends during studies.

Using a small test file (.txt format) and ggplot, I have been able to produce several plots in a PDF file; all my plots use time (days) since start of study as the x-axis.

My next step, and the one which I am stuck on, it working with dates in the dataset to compute time since start of study; this new variable would be my X-axis data for all plots. I have attached a test file that I am working with. Format of dataset is date, hours, mins, secs, V1, V2. I am able to change the date to any format (YYYY-MM-DD, dd/MM/YY) before importing into RStudio - I have tried many different options both in format and RStudio import options (ie. read table, csv)

What I want to do is create a new variable (NewVar) from dataset location [1,1] (ie. the first date the study began) and transform the date to a number. Was trying this:

NewVar <- dataset [1,1]
transformed.dataset <- transform(dataset, NewDate = V1 - NewVar)

  • V1= date column

The errors I receive, depending upon how I import / arrange date are either :

  • In Ops.factor(V1, NewVar) : ‘-’ not meaningful for factors
  • non-numeric argument to binary operator

I saw online examples on how to calculate timediff, but none that involve transform function. I'm guessing I'm not presenting the date to Rstudio in a format that it can perform a mathematical function.

My ultimate goal is to add days (from date calculation) + time of day (hh,mm,ss as a fraction of a day) to produce an X-value for each variable ; I have worked out the time of day function :slight_smile:

Any help would be appreciated. I can provide more code if needed

Thanks in advance, GooberBoy

Using transform is not really recommended. See the warning under ?transform

For date manipulations you can use lubridate:

Possibly hms, too, if you need it:

1 Like

Providing a reproducible example is not only a good thing to do, it also increases your chances of getting help.

Could you ask this with a minimal REPRoducible EXample (reprex)? A reprex makes it much easier for others to understand your issue and figure out how to help.

If you've never heard of a reprex before, you might want to start by reading this FAQ:

1 Like

lubridate library worked! thanks for guidance Martin.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.