[R] dataframe, transform, strsplit
Gabor Grothendieck
ggrothendieck at gmail.com
Mon Oct 25 19:09:23 CEST 2010
On Mon, Oct 25, 2010 at 12:53 PM, Matthew Pettis
<matthew.pettis at gmail.com> wrote:
> Hi,
>
> I have a dataframe that has a column of vectors that I need to extract off
> the character string before the first '.' character and put it into a
> separate column. I thought I could use 'strsplit' for it within
> 'transform', but I can't seem to get the right invocation. Here is a sample
> dataframe that has what I have, what I want, and what I get. Can someone
> tell me how to get what is in the 'want' column from the 'have' column
> programatically?
>
> tia,
> Matt
>
> df <- data.frame(have=c("a.b.c", "d.e.f", "g.h.i"), want=c("a","d","g"))
> df.xform <- transform(df, get=strsplit(as.character(have), split=".",
> fixed=TRUE)[[1]][1])
> df.xform
>
Try replacing the dot [.] and everything thereafter .* with nothing ""
like this:
transform(df, want = sub("[.].*", "", have))
--
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com
More information about the R-help
mailing list