[R] Combining character, numeric and dates fields in one data frame

Jim Lemon jim at bitwrit.com.au
Fri Jun 27 00:41:04 CEST 2014


On Thu, 26 Jun 2014 10:42:54 AM Mangalani Peter Makananisa wrote:
> Hi,
> 
> I am try to fix the data frame and I see that we only have two fields
> defined characters and  numeric fields. How do I add date field   on 
the
> same data after using the as.Date  &  as.yearmon and be able to 
manipulate
> the data on the date field(s)?
> 
> Please advise
> 
> 
> Kind regards
> 
> peter
> 
Hi peter,
Let's say the two fields in your data frame are:

mydata$date<-c("2014-06-01","2014-06-02",...,"2014-06-30")
mydata$mintemp<-c(14,17,...,12)

If you want to change the character dates to date objects:

mydata$date<-as.Date(mydata$date,"%Y-%m-%d")

If your data frame doesn't contain the dates already:

mydata$weather<-c("clear","cloudy",...,"rain")
mydata$mintemp<-c(14,17,...,12)

and you have a vector of the dates as character fields:

mydates<-c("2014-06-01","2014-06-02",...,"2014-06-30")

mydata$date<-as.Date(mydates,"%Y-%m-%d")

Jim



More information about the R-help mailing list