Skip to content

about data format in R

12 messages · lily li, Sarah Goslee, Rui Barradas +3 more

#
Hi R users,

I'm trying to read in data, and then plot time series data. However, I have
some problems. In my dataset, the first column represents time, and in the
format:
mm/dd/yyyy-hr:min:sec; For example, 10/01/1995-00:00:00,
10/01/1995-06:00:00, etc.

df:
           date                    evap     precip    intercept
10/01/1995-00:00:00       1.5          2            0.2
10/01/1995-12:00:00       1.7          2.2         0.1
10/02/1995-00:00:00       1.5          1.8         0.3
...

My code is like this
file1 = read.table('df', head=T)

When I read in data, I found that it read incorrectly. How to format when
read in data? Thanks.
#
Hello,

Have you tried

df$date <- as.POSIXct(dat$date, format = "%m/%d/%Y-%H:%M:%S")

?

Hope this helps,

Rui Barradas


Em 30-12-2016 17:40, lily li escreveu:
#
Hi Rui,

Thanks for your reply. When I read in data using my code, the first column
ranges from 0 to 1. So when I use the code you wrote, it shows the error
message:
Error in as.POSIXct.numeric(DF$Date, format = "%m/%d/%Y-%H:%M:%S") :
'origin' must be supplied
On Fri, Dec 30, 2016 at 11:23 AM, Rui Barradas <ruipbarradas at sapo.pt> wrote:

            

  
  
#
Probably you need to use

file1 <- read.table('df', header=TRUE, stringsAsFactors=FALSE)

str(file1)

generally shows you all sorts of useful things about the file you have
just imported into R.

Sarah
On Fri, Dec 30, 2016 at 1:37 PM, lily li <chocold12 at gmail.com> wrote:
#
Hello,

What I did was:

DF <- read.table(text = "
date                    evap     precip    intercept
10/01/1995-00:00:00       1.5          2            0.2
10/01/1995-12:00:00       1.7          2.2         0.1
10/02/1995-00:00:00       1.5          1.8         0.3
", header = TRUE, stringsAsFactors = FALSE)

str(DF)

DF$date <- as.POSIXct(DF$date, format = "%m/%d/%Y-%H:%M:%S")
str(DF)

And it worked with no errors.

Rui Barradas

Em 30-12-2016 18:47, Sarah Goslee escreveu:
#
Hi

Is this the output from Excel?
If so format it in Excel for a date format not a date-time format . 
Depending how the dates were inputted into Excel and the Excel setup a date
may not be a date format.
There are no rules with microsoft formatting so beware!


Regards

Duncan

Duncan Mackay
Department of Agronomy and Soil Science
University of New England
Armidale NSW 2351
Email: home: mackay at northnet.com.au

-----Original Message-----
From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of lily li
Sent: Saturday, 31 December 2016 05:38
To: Rui Barradas
Cc: R mailing list
Subject: Re: [R] about data format in R

Hi Rui,

Thanks for your reply. When I read in data using my code, the first column
ranges from 0 to 1. So when I use the code you wrote, it shows the error
message:
Error in as.POSIXct.numeric(DF$Date, format = "%m/%d/%Y-%H:%M:%S") :
'origin' must be supplied
On Fri, Dec 30, 2016 at 11:23 AM, Rui Barradas <ruipbarradas at sapo.pt> wrote:

            
the
______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
#
Hi all,

Thanks for your help. Now I can convert data to the format "yyyy-mm-dd
hh:mm:ss", but how to convert it to "yyyy-mm-dd"? The datasets are txt
files, not from excel.
On Fri, Dec 30, 2016 at 3:01 PM, Duncan Mackay <dulcalma at bigpond.com> wrote:

            

  
  
#
?substring

(among others)

-- Bert
Bert Gunter

"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
On Sat, Dec 31, 2016 at 9:26 AM, lily li <chocold12 at gmail.com> wrote:
#
Thanks. It seems that substrings only subset the data, not convert the
format. For example, I use the code below:
DF$time = substring(DF$time, '%Y-%m-%d')

where DF$time has the structure:
'2002-01-01 00:00:00', '2002-01-01 12:00:00', '2003-01-01 00:00:00',
'2003-01-01 12:00:00', etc.
I wanted to convert the 'time' column to '2002-01-01', '2002-01-01',
'2003-01-01', '2003-01-01', etc.

Using the code above, it gives the error message:
Error in as.POSIXlt.character(as.character(x), ...) :
  character string is not in a standard unambiguous format

On Sat, Dec 31, 2016 at 10:36 AM, Bert Gunter <bgunter.4567 at gmail.com>
wrote:

  
  
#
Use: as.Date
David Winsemius
Alameda, CA, USA
#
I tried it, but it doesn't work. The time column is blank then.
DF$time = substring(DF$time, first=as.Date('1999-01-01),
last=as.Date('2005-12-30'))

On Sat, Dec 31, 2016 at 10:52 AM, David Winsemius <dwinsemius at comcast.net>
wrote:

  
  
#
Sorry, the problem has been solved. I found that strptime is a good
function for this.
DF$time2 = strptime(DF$time, format='%Y-%m-%d)
On Sat, Dec 31, 2016 at 11:02 AM, lily li <chocold12 at gmail.com> wrote: