Michael,

Your suggestion to use sapply solves the problem.Thanks so much for your help


Mauricio


________________________________
 From: R. Michael Weylandt <michael.weylandt@gmail.com>

Cc: "r-help@r-project.org" <r-help@r-project.org> 
Sent: Friday, March 30, 2012 8:33 PM
Subject: Re: [R] How to use access results of gregexpr in data frames

I think when you assign to dframe$all there's more going on than you
realize: you're actually using a multi-element list as a column of a
data frame which is, while possible, perhaps nonstandard. Perhaps you
want this:

dframe$all <- t(simplify2array(gregexpr("/", dframe[, 1])))
print(dframe)
#       date x1 all.1 all.2
# 1 5/14/2011  2     2     5
# 2  4/7/2011  2     2     4

If you just want the location of the second slash, this works:

dframe$x2 <- sapply(gregexpr("/", dframe[, 1]),`[`,2)

Hope this helps,

Michael

On Fri, Mar 30, 2012 at 7:14 PM, Mauricio Cornejo

> 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]]
>
>
> ______________________________________________
> R-help@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.
>
	[[alternative HTML version deleted]]

