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. Is there an R function or workaround to do this? Ken kmnanus at gmail.com 914-450-0816 (tel) 347-730-4813 (fax)
Like this? x <- factor("001-014") y <- substr(as.character(x), 1, 3) Best, Ista On Thu, Mar 3, 2016 at 3:18 PM, KMNanus <kmnanus at gmail.com> 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. > > Is there an R function or workaround to do this? > > > Ken > kmnanus at gmail.com > 914-450-0816 (tel) > 347-730-4813 (fax) > > > > ______________________________________________ > R-help at 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.
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 > kmnanus at gmail.com > 914-450-0816 (tel) > 347-730-4813 (fax) > > > > ______________________________________________ > R-help at 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: hpages at fredhutch.org Phone: (206) 667-5791 Fax: (206) 667-1319
When I do that, I get "Error in `$<-.data.frame`(`*tmp*`, "site", value = integer(0)) : replacement has 0 rows, data has 6? The data frame has 6 rows. Ken kmnanus at gmail.com 914-450-0816 (tel) 347-730-4813 (fax)> On Mar 3, 2016, at 4:52 PM, Herv? Pag?s <hpages at 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 >> kmnanus at gmail.com >> 914-450-0816 (tel) >> 347-730-4813 (fax) >> >> >> >> ______________________________________________ >> R-help at 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: hpages at fredhutch.org > Phone: (206) 667-5791 > Fax: (206) 667-1319
When I did that, I got - "Error in `$<-.data.frame`(`*tmp*`, "site", value = integer(0)) : replacement has 0 rows, data has 6? The data frame has 6 rows. Ken kmnanus at gmail.com 914-450-0816 (tel) 347-730-4813 (fax)> On Mar 3, 2016, at 4:14 PM, Ista Zahn <istazahn at gmail.com> wrote: > > Like this? > > x <- factor("001-014") > y <- substr(as.character(x), 1, 3) > > Best, > Ista > > > > > On Thu, Mar 3, 2016 at 3:18 PM, KMNanus <kmnanus at gmail.com> 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. >> >> Is there an R function or workaround to do this? >> >> >> Ken >> kmnanus at gmail.com >> 914-450-0816 (tel) >> 347-730-4813 (fax) >> >> >> >> ______________________________________________ >> R-help at 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.