5

My post was apparently unclear, so I'm trying to fix it, don't hesitate to tell me if I'm still unclear!

I got a dataframe of physical variables, with a data every minute. I'd like to convert the 4 first columns into a single one: "%d/%m/%Y %H:%M" (GMT) in R.

Year    Julian_day  Hour    Minute  Air_temp    Water_temp  Rel_hum   Wind_int  Wind_dir
2012    1   0   0   11.82   4.73    87.2    5.1 310
2012    1   0   1   11.92   4.743   87.2    5   310
2012    1   0   2   11.86   4.748   86.9    4.7 310
M--
  • 25,431
  • 8
  • 61
  • 93
Doc Martin's
  • 341
  • 1
  • 3
  • 11
  • Please provide a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – Thomas Jul 30 '13 at 13:20
  • Perhaps harsh to mark down a Q1 newbie? The point of the [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) is that it clarifies things like what object type you're using. Welcome to SO Doc. – geotheory Jul 30 '13 at 13:30
  • Sorry for my post, I modified it and hope this new one is clearer. – Doc Martin's Jul 30 '13 at 14:02
  • As far as I understand the Julian system, Julian day 1 is 1 January 4713 BC. Please explain the meaning of your column labeled "Julian_day." Is it simply the first day of the year? – James Pringle Jul 30 '13 at 14:14
  • Yes, here the Julian_day is the first day of the year, so I have days from 1 to 365 and I'd like to have normal days from 1 to 28-31 depending on the month. – Doc Martin's Jul 30 '13 at 14:32

1 Answers1

10
# This imports your data into a variable named "weather"
weather <- read.table(text = "Year    Julian_day  Hour    Minute  Air_temp    Water_temp  Rel_hum   Wind_int  Wind_dir
2012    1   0   0   11.82   4.73    87.2    5.1 310
2012    1   0   1   11.92   4.743   87.2    5   310
2012    1   0   2   11.86   4.748   86.9    4.7 310", header = TRUE)

# Combine the first four columns into a character vector
date_info <- with(weather, paste(Year, Julian_day, Hour, Minute))
# Parse that character vector
strptime(date_info, "%Y %j %H %M")
Richie Cotton
  • 118,240
  • 47
  • 247
  • 360
  • Thank you for your answerand your time, however I still have 1 problems. I don't understand the principle of textConnection, and I wonder if it's the good solution to solve my problem given the fact that I have one row for each minute of a year, so it's a huge dataset.I'd like to combine the 4 columns to 1 new column. – Doc Martin's Jul 30 '13 at 15:27
  • 1
    (1) `textConnection` is just a way to set up the sample data; (2) 525600 rows isn't *that* bad. You can drop the four columns from the data frame after creating `date_info`, if you really need to save space. Try this out and see if it works ... – Ben Bolker Jul 30 '13 at 16:09
  • Nice way! Thank you. – igorkf Mar 24 '21 at 15:06