Sichong Chen
2012-Mar-09 05:32 UTC
[R] From (common IDs different Names) To (common IDs common Names)
Dear Community I have a large dataframe x as follows with common ids but different names:> x <- data.frame(ID = c(1,1,2,2,2,3,3),+ Name = c("B Branch A Firm ","A Firm","B Firm","B Firm","B Firm C Branch","C Firm","C Firm A Branch") + )> xID Name 1 1 B Branch A Firm 2 1 A Firm 3 2 B Firm 4 2 B Firm 5 2 B Firm C Branch 6 3 C Firm 7 3 C Firm A Branch Q: How can I turn it into a dataframe with common id and common names, like this:> y <- data.frame(ID = c(1,1,2,2,2,3,3),+ Name = c("A Firm","A Firm","B Firm","B Firm","B Firm","C Firm","C Firm") + )> yID Name 1 1 A Firm 2 1 A Firm 3 2 B Firm 4 2 B Firm 5 2 B Firm 6 3 C Firm 7 3 C Firm Although I searched a lot, I am still not able to find answers to my question. Please help. Thanks in advance. 2012-03-09 Chen [[alternative HTML version deleted]]
R. Michael Weylandt
2012-Mar-09 11:45 UTC
[R] From (common IDs different Names) To (common IDs common Names)
library(stringr) x$Name <- str_trim(gsub("[ABC] Branch", "",x$Name)) Michael On Fri, Mar 9, 2012 at 12:32 AM, Sichong Chen <cscwin at gmail.com> wrote:> Dear Community > > I have a large dataframe x as follows with common ids but different names: > >> x <- data.frame(ID = c(1,1,2,2,2,3,3), > + Name = c("B Branch A Firm ","A Firm","B Firm","B Firm","B Firm C Branch","C Firm","C Firm A Branch") > + ) >> x > ?ID ? ? ? ? ? ? Name > 1 ?1 B Branch A Firm > 2 ?1 ? ? ? ? ? A Firm > 3 ?2 ? ? ? ? ? B Firm > 4 ?2 ? ? ? ? ? B Firm > 5 ?2 ?B Firm C Branch > 6 ?3 ? ? ? ? ? C Firm > 7 ?3 ?C Firm A Branch > > Q: How can I turn it into a dataframe with common id and common names, like this: > >> y <- data.frame(ID = c(1,1,2,2,2,3,3), > + Name = c("A Firm","A Firm","B Firm","B Firm","B Firm","C Firm","C Firm") > + ) >> y > ?ID ? Name > 1 ?1 A Firm > 2 ?1 A Firm > 3 ?2 B Firm > 4 ?2 B Firm > 5 ?2 B Firm > 6 ?3 C Firm > 7 ?3 C Firm > > Although I searched a lot, I am still not able to find answers to my question. ?Please help. > > Thanks in advance. > > 2012-03-09 > > > > Chen > ? ? ? ?[[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.