Hello,
I'm trying to figure out how to find the index of the second occurrence of "/"
in a string (which happens to represent a date) within a data frame column.
I've used the following code successfully to find the first instance of "/".
dframe <- data.frame(date=c("5/14/2011", "4/7/2011"))
dframe$x1 <- regexpr("/", dframe[, 1])
dframe
date x1
1 5/14/2011 2
2 4/7/2011 2To find the second instance, I thought I'd try to use gregexpr to
find all instances of "/" (there's always two per string).
dframe$all <- gregexpr("/", dframe[, 1])
dframe
date x1 all
1 5/14/2011 2 2,5
2 4/7/2011 2 2,4
So far so good. I then thought to index the second element of dframe$all. I
tried both of the following unsuccessfully.
dframe$x2 <- dframe[, "all"][[2]]
dframe$x2 <- dframe[, "all"][2]
The desired final output is as follows ... but I don't know how to get there.
date x1 all x2
1 5/14/2011 2 2,5 5
2 4/7/2011 2 2,4 4
Many thanks for your help,
Mauricio
[[alternative HTML version deleted]]
______________________________________________
[email protected] 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.