Hi Ken,

You do that with as.factor(), as has already been suggested. You'll need to
provide a reproducible example to show us what's going wrong. Using fake
data is fine, we just need to see some data that look like yours and the
code you're using.

Sarah

On Fri, Mar 4, 2016 at 11:57 AM, KMNanus <kmna...@gmail.com> wrote:

> Let me see if I can ask the question more clearly - I am trying to extract
> a section of a hyphenated factor. For example, 001-004 is one observation
> of test$ken, which is a factor, and I want to set up a new factor variable
> called place that would have 001 as an observation. If I call mutate(place
> = (as.character (test$ken)), I can extract 001 from  001-004, but but don't
> know how to subsequently convert that character string back into a factor.
>
>
> Or can 001 be extracted from a factor as a factor?
>
> Do you know how to execute either of these approaches?
>
> Ken
> kmna...@gmail.com
> 914-450-0816 (tel)
> 347-730-4813 (fax)
>
>
>
> > On Mar 3, 2016, at 8:33 PM, Hervé Pagès <hpa...@fredhutch.org> wrote:
> >
> > On 03/03/2016 02:13 PM, KMNanus wrote:
> >> When I do that,
> >
> > When you do what exactly?
> >
> > It's impossible for anyone here to know what you're doing if you
> > don't show the code.
> >
> >> I get "Error in `$<-.data.frame`(`*tmp*`, "site", value
> >> = integer(0)) :
> >>   replacement has 0 rows, data has 6”
> >>
> >> The data frame has 6 rows.
> >
> > You said you had a factor variable, you never mentioned you had a
> > data.frame. If the factor variable is part of a data.frame 'df',
> > then first extract it with something like df$myvar or df[["myvar"]],
> > and then call substr() followed by as.factor() on it.
> >
> > H.
> >
> >>
> >> Ken
> >> kmna...@gmail.com <mailto:kmna...@gmail.com>
> >> 914-450-0816 (tel)
> >> 347-730-4813 (fax)
> >>
> >>
> >>> On Mar 3, 2016, at 4:52 PM, Hervé Pagès <hpa...@fredhutch.org
> >>> <mailto:hpa...@fredhutch.org>> wrote:
> >>>
> >>> Hi,
> >>>
> >>> On 03/03/2016 12:18 PM, KMNanus wrote:
> >>>> I have a factor variable that is 6 digits and hyphenated.  For
> >>>> example, 001-014.
> >>>>
> >>>> I need to extract the first 3 digits to a new variable using mutate
> >>>> in dplyr - in this case 001 - but can’t find a function to do it.
> >>>>
> >>>> substr will do this for character strings, but I need the variable to
> >>>> remain as a factor.
> >>>
> >>> What prevents you from calling as.factor() on the result to turn it
> >>> back into a factor?
> >>>
> >>> H.
> >>>
> >>>>
> >>>> Is there an R function  or workaround to do this?
> >>>>
> >>>>
> >>>> Ken
> >>>> kmna...@gmail.com <mailto:kmna...@gmail.com>
> >>>> 914-450-0816 (tel)
> >>>> 347-730-4813 (fax)
> >>>>
> >>>>
> >>>>
> >>>> ______________________________________________
> >>>> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> >>>> 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.
> >>>>
> >>>
> >>> --
> >>> Hervé Pagès
> >>>
> >>> Program in Computational Biology
> >>> Division of Public Health Sciences
> >>> Fred Hutchinson Cancer Research Center
> >>> 1100 Fairview Ave. N, M1-B514
> >>> P.O. Box 19024
> >>> Seattle, WA 98109-1024
> >>>
> >>> E-mail: hpa...@fredhutch.org <mailto:hpa...@fredhutch.org>
> >>> Phone:  (206) 667-5791
> >>> Fax:    (206) 667-1319
> >>
> >
> > --
> > Hervé Pagès
> >
> > Program in Computational Biology
> > Division of Public Health Sciences
> > Fred Hutchinson Cancer Research Center
> > 1100 Fairview Ave. N, M1-B514
> > P.O. Box 19024
> > Seattle, WA 98109-1024
> >
> > E-mail: hpa...@fredhutch.org
> > Phone:  (206) 667-5791
> > Fax:    (206) 667-1319
>
>

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.

Reply via email to