Hi, I have a large data frame and within this there is one column which contains individual codes (eg. 1.1234.2a.2). I am splitting these codes into their 4 components using strsplit (eg. "1", "1234", "2a", "2"). However there are some individual codes which do not have a last component (eg. 2.4356.3b. ), I want to give these codes a "1" as their last component (eg. 2.4356.3b.1) but I can't get it to work. This is my example and attempt: sim.code<-c("1.1234.1a.1","1.1234.1a.2","1.3245.2c.5","4.6743.3c.","4.3254.6b.4","3.5463.2a.") sim.val<-c(4,5,3,6,4,7) sim.df<-data.frame(sim.code,sim.val) sim.df$sim.code2<-sub(".$",".1",sim.df$sim.code) sim.df but this changes all the ends to ".1" Thanks, Chris -- View this message in context: http://r.789695.n4.nabble.com/Substitute-value-tp4660699.html Sent from the R help mailing list archive at Nabble.com.
Hi, try this: sim.df<-data.frame(sim.code,sim.val,stringsAsFactors=FALSE) sim.df[,1][-grep("\\d+$",sim.df[,1])]<- paste(sim.df[,1][-grep("\\d+$",sim.df[,1])] , 1,sep="") sim.df # ? ? sim.code sim.val #1 1.1234.1a.1 ? ? ? 4 #2 1.1234.1a.2 ? ? ? 5 #3 1.3245.2c.5 ? ? ? 3 #4 4.6743.3c.1 ? ? ? 6 #5 4.3254.6b.4 ? ? ? 4 #6 3.5463.2a.1 ? ? ? 7 A.K. ----- Original Message ----- From: chris201 <chris.r.bennett at btinternet.com> To: r-help at r-project.org Cc: Sent: Friday, March 8, 2013 5:23 AM Subject: [R] Substitute value Hi, I have a large data frame and within this there is one column which contains individual codes (eg. 1.1234.2a.2).? I am splitting these codes into their 4 components using strsplit (eg. "1", "1234", "2a", "2").? However there are some individual codes which do not have a last component (eg. 2.4356.3b. ), I want to give these codes a "1" as their last component (eg. 2.4356.3b.1) but I can't get it to work. This is my example and attempt: sim.code<-c("1.1234.1a.1","1.1234.1a.2","1.3245.2c.5","4.6743.3c.","4.3254.6b.4","3.5463.2a.") sim.val<-c(4,5,3,6,4,7) sim.df<-data.frame(sim.code,sim.val) sim.df$sim.code2<-sub(".$",".1",sim.df$sim.code) sim.df but this changes all the ends to ".1" Thanks, Chris -- View this message in context: http://r.789695.n4.nabble.com/Substitute-value-tp4660699.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ 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> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of chris201 > Sent: Friday, March 08, 2013 11:23 AM > To: r-help at r-project.org > Subject: [R] Substitute value > > Hi, > I have a large data frame and within this there is one column which > contains individual codes (eg. 1.1234.2a.2). I am splitting these > codes into their 4 components using strsplit (eg. "1", "1234", "2a", > "2"). However there are some individual codes which do not have a last > component (eg. 2.4356.3b. ), I want to give these codes a "1" as their > last component (eg. 2.4356.3b.1) but I can't get it to work. > > This is my example and attempt: > > sim.code<- > c("1.1234.1a.1","1.1234.1a.2","1.3245.2c.5","4.6743.3c.","4.3254.6b.4", > "3.5463.2a.") > sim.val<-c(4,5,3,6,4,7) > sim.df<-data.frame(sim.code,sim.val) > > sim.df$sim.code2<-sub(".$",".1",sim.df$sim.code) > sim.df > > but this changes all the ends to ".1"It is not perfect solution but lll<-strsplit(sim.code, ".", fixed=T) gives you a list of splitted data index<-which(sapply(lll, length)==3) gives you values of list with only 3 elements after that some clever solution must exist but if the list is not big I would go further with for cycle for(i in index) lll[[i]] <-c(lll[[i]], 1) This put 1 in each empty slot of list lll. After that you cen either to put everything back by paste or do whatever you like. Regards Petr> > Thanks, > Chris > > > > -- > View this message in context: http://r.789695.n4.nabble.com/Substitute- > value-tp4660699.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.
On Fri, Mar 8, 2013 at 5:23 AM, chris201 <chris.r.bennett at btinternet.com> wrote:> Hi, > I have a large data frame and within this there is one column which contains > individual codes (eg. 1.1234.2a.2). I am splitting these codes into their 4 > components using strsplit (eg. "1", "1234", "2a", "2"). However there are > some individual codes which do not have a last component (eg. 2.4356.3b. ), > I want to give these codes a "1" as their last component (eg. 2.4356.3b.1) > but I can't get it to work. > > This is my example and attempt: > > sim.code<-c("1.1234.1a.1","1.1234.1a.2","1.3245.2c.5","4.6743.3c.","4.3254.6b.4","3.5463.2a.") > sim.val<-c(4,5,3,6,4,7) > sim.df<-data.frame(sim.code,sim.val) > > sim.df$sim.code2<-sub(".$",".1",sim.df$sim.code) > sim.df > > but this changes all the ends to ".1" >1. Use "\\.$" like this: sim.code.1 <- sub("\\.$", ".1", sim.code) read.table(text = sim.code.1, sep = ".") 2. or without regular expressions DF <- read.table(text = sim.code, sep = ".") DF[is.na(DF)] <- "1" -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com
Hi, Using your code: sub("[.]$",".1",sim.code) #[1] "1.1234.1a.1" "1.1234.1a.2" "1.3245.2c.5" "4.6743.3c.1" "4.3254.6b.4" #[6] "3.5463.2a.1" A.K. ----- Original Message ----- From: chris201 <chris.r.bennett at btinternet.com> To: r-help at r-project.org Cc: Sent: Friday, March 8, 2013 5:23 AM Subject: [R] Substitute value Hi, I have a large data frame and within this there is one column which contains individual codes (eg. 1.1234.2a.2).? I am splitting these codes into their 4 components using strsplit (eg. "1", "1234", "2a", "2").? However there are some individual codes which do not have a last component (eg. 2.4356.3b. ), I want to give these codes a "1" as their last component (eg. 2.4356.3b.1) but I can't get it to work. This is my example and attempt: sim.code<-c("1.1234.1a.1","1.1234.1a.2","1.3245.2c.5","4.6743.3c.","4.3254.6b.4","3.5463.2a.") sim.val<-c(4,5,3,6,4,7) sim.df<-data.frame(sim.code,sim.val) sim.df$sim.code2<-sub(".$",".1",sim.df$sim.code) sim.df but this changes all the ends to ".1" Thanks, Chris -- View this message in context: http://r.789695.n4.nabble.com/Substitute-value-tp4660699.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ 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.