[R] How to use access results of gregexpr in data frames

R. Michael Weylandt michael.weylandt at gmail.com
Sat Mar 31 02:33:04 CEST 2012


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
<mauriciocornejo at yahoo.com> wrote:
> 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 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