[R] Fw: split date and time
arun
smartpink111 at yahoo.com
Sat Apr 13 00:30:02 CEST 2013
----- Forwarded Message -----
From: arun <smartpink111 at yahoo.com>
To: Ye Lin <yelin at lbl.gov>
Cc:
Sent: Friday, April 12, 2013 6:25 PM
Subject: Re: [R] split date and time
Hi Ye,
Is this okay?
dat2<-cbind(dat1[,-2],do.call(rbind,strsplit(dat1[,2]," ")),stringsAsFactors=FALSE)
dat2
# Number Value 1 2
#1 1 1 1/1/2013 0:00
#2 2 2 1/1/2013 0:01
#3 3 3 1/1/2013 0:03
colnames(dat2)[3:4]<- c("Date","Time")
dat2
# Number Value Date Time
#1 1 1 1/1/2013 0:00
#2 2 2 1/1/2013 0:01
#3 3 3 1/1/2013 0:03
str(dat2)
#'data.frame': 3 obs. of 4 variables:
#$ Number: int 1 2 3
#$ Value : int 1 2 3
#$ Date : chr "1/1/2013" "1/1/2013" "1/1/2013"
#$ Time : chr "0:00" "0:01" "0:03"
________________________________
From: Ye Lin <yelin at lbl.gov>
To: arun <smartpink111 at yahoo.com>
Sent: Friday, April 12, 2013 6:16 PM
Subject: Re: [R] split date and time
What if I have many columns, for example I have 50 columns in dat1, and say the "TimeStamp" column is the 10th, anyway to do that instead of listing all the other columns when building dat2?
Thanks!
On Fri, Apr 12, 2013 at 3:08 PM, arun <smartpink111 at yahoo.com> wrote:
Hi,
>dat1<- read.table(text="
>Number,TimeStamp,Value
>1,1/1/2013 0:00,1
>2,1/1/2013 0:01,2
>3,1/1/2013 0:03,3
>",sep=",",header=TRUE,stringsAsFactors=FALSE)
>dat2<-data.frame(Number=dat1[,1],do.call(rbind,strsplit(dat1[,2]," ")), Value=dat1[,3])
> names(dat2)[2:3]<- c("Date","Time")
> dat2
># Number Date Time Value
>#1 1 1/1/2013 0:00 1
>#2 2 1/1/2013 0:01 2
>#3 3 1/1/2013 0:03 3
>A.K.
>
>
>
>
>
>----- Original Message -----
>From: Ye Lin <yelin at lbl.gov>
>To: R help <r-help at r-project.org>
>Cc:
>Sent: Friday, April 12, 2013 5:49 PM
>Subject: [R] split date and time
>
>Hi R experts,
>
>For example I have a dataset looks like this:
>
>
>Number TimeStamp Value
>1 1/1/2013 0:00 1
>2 1/1/2013 0:01 2
>3 1/1/2013 0:03 3
>
>How can I split the "TimeStamp" Column into two and return a new table like
>this:
>
>Number Date Time Value
>1 1/1/2013 0:00 1
>2 1/1/2013 0:01 2
>3 1/1/2013 0:03 3
>
>Thank!
>
> [[alternative HTML version deleted]]
>
>______________________________________________
>R-help at r-project.org mailing list
>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.
>
>
More information about the R-help
mailing list