Hey, I have a dataset and I want to identify the records by groups for further use in ggplot. Here is a sample data: ID Value AL1 1 AL2 2 CA1 3 CA4 4 I want to identify all the records that in the same state (AL1 AND A2), group them as "AL", and do the same for CA1 and CA4. How can I have an output like: ID Value State AL1 1 AL AL2 2 AL CA1 3 CA CA4 4 CA Thanks for your help! [[alternative HTML version deleted]]
Hello, Try the following. dat <- read.table(text = " ID Value AL1 1 AL2 2 CA1 3 CA4 4 ", header = TRUE, stringsAsFactors = FALSE) dat$State <- substr(dat$ID, 1, 2) Note that this dependes on having State being defined by the first two characters of ID. Hope this helps, Rui Barradas Em 11-04-2013 19:37, Ye Lin escreveu:> Hey, > > I have a dataset and I want to identify the records by groups for further > use in ggplot. > > Here is a sample data: > > ID Value > AL1 1 > AL2 2 > CA1 3 > CA4 4 > > I want to identify all the records that in the same state (AL1 AND A2), > group them as "AL", and do the same for CA1 and CA4. How can I have an > output like: > > ID Value State > AL1 1 AL > AL2 2 AL > CA1 3 CA > CA4 4 CA > > Thanks for your help! > > [[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. >
Hi, dat1<- read.table(text=" ID? Value AL1? 1 AL2? 2 CA1? 3 CA4? 4 ",sep="",header=TRUE,stringsAsFactors=FALSE) dat2<- dat1 dat1$State<-gsub("\\d+","",dat1$ID) dat1 #?? ID Value State #1 AL1???? 1??? AL #2 AL2???? 2??? AL #3 CA1???? 3??? CA #4 CA4???? 4??? CA #or library(stringr) dat2$State<-str_replace(dat2$ID,"\\d+","") A.K. Hey, I have a dataset and I want to identify the records by groups for further use in ggplot. Here is a sample data: ID ?Value AL1 ?1 AL2 ?2 CA1 ?3 CA4 ?4 I want to identify all the records that in the same state (AL1 AND A2), group them as "AL", and do the same for CA1 and CA4. How can I have an output like: ID ?Value State AL1 ?1 ? ? ? AL AL2 ?2 ? ? ?AL CA1 ?3 ? ? ?CA CA4 ?4 ? ? CA Thanks for your help!