CoryB
CoryB

Reputation: 85

Extracting parts of a factor

I am trying to extract parts of a factor within a data.frame that looks like following:

aug$V5 <- c("00:00:00","01:01:05","04:32:22")

The format is in a factor and I want to extract the first two digits, as they are the hours that I am interested in and place them in a different column within my vector:

Output looks like this:

aug$hours <- c("00","01","04")

I think I just need to use the sub function but I am unsure.

Thanks.

Upvotes: 1

Views: 112

Answers (4)

rnso
rnso

Reputation: 24593

Another solution:

V5 <- factor(c("00:00:00","01:01:05","04:32:22"))

data.frame(strsplit(as.character(V5), ':'))[1,]
  c..00....00....00.. c..01....01....05.. c..04....32....22..
1                  00                  01                  04

One can just ignore the names above, or:

setNames(data.frame(strsplit(as.character(V5), ':'))[1,],seq(length(V5)))
   1  2  3
1 00 01 04

Even better would be following as suggested by @ Ananda Mahto in comments below:

sapply(strsplit(as.character(V5), ':'), '[[', 1) 
[1] "00" "01" "04"

Upvotes: 0

Rich Scriven
Rich Scriven

Reputation: 99361

lubridate to the rescue

> v <- c("00:00:00","01:01:05","04:32:22")
> library(lubridate)
> hour(hms(factor(v)))
[1] 0 1 4

or just take a substring

> substr(factor(v),1,2)
[1] "00" "01" "04"

Upvotes: 0

David Arenburg
David Arenburg

Reputation: 92302

Or using some base R dates manipulations

V5 <- factor(c("00:00:00","01:01:05","04:32:22"))
as.POSIXlt(paste(Sys.Date(), V5))$hour
## [1] 0 1 4

Upvotes: 1

MrFlick
MrFlick

Reputation: 206446

If i create a simple example factor here

V5 <- factor(c("00:00:00","01:01:05","04:32:22"))

then running

gsub("^(\\d{2}).*","\\1",as.character(V5))

returns

[1] "00" "01" "04"

as desired.

Upvotes: 0

Related Questions