Hello R experts, I have go this data frame: 'data.frame': 1 obs. of 20 variables: $ Anno : chr "PREVISIONI VS TARGET" $ OreTot: num 41 $ GioTot: logi NA $ OrGTot: logi NA $ OreCli: num 99 $ GioCli: logi NA $ OrGCli: logi NA $ OreFor: num -27 $ GioFor: logi NA $ OrGFor: logi NA $ OreOrt: num -18 $ GioOrt: logi NA $ OrGOrt: logi NA $ OreSpo: num -6 $ GioSpo: logi NA $ OrGSpo: logi NA $ OreUff: num -7 $ GioUff: logi NA $ OrGUff: logi NA $ temp : num 0 Is there any way I can format the numeric fields so that I get a leading "+" whenever the value is > 0? In the specific case I would need something like: 'data.frame': 1 obs. of 20 variables: $ Anno : chr "PREVISIONI VS TARGET" $ OreTot: num +41 $ GioTot: logi NA $ OrGTot: logi NA $ OreCli: num +99 $ GioCli: logi NA $ OrGCli: logi NA $ OreFor: num -27 $ GioFor: logi NA $ OrGFor: logi NA $ OreOrt: num -18 $ GioOrt: logi NA $ OrGOrt: logi NA $ OreSpo: num -6 $ GioSpo: logi NA $ OrGSpo: logi NA $ OreUff: num -7 $ GioUff: logi NA $ OrGUff: logi NA $ temp : num 0 Thank you in advance, Luca Mr. Luca Meyer www.lucameyer.com R version 2.15.1 Mac OS X 10.8 [[alternative HTML version deleted]]
Hi> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Luca Meyer > Sent: Thursday, August 30, 2012 7:55 AM > To: R help > Subject: [R] Leading plus in numeric fields > > Hello R experts, > > I have go this data frame: > > 'data.frame': 1 obs. of 20 variables: > $ Anno : chr "PREVISIONI VS TARGET" > $ OreTot: num 41 > $ GioTot: logi NA > $ OrGTot: logi NA > $ OreCli: num 99 > $ GioCli: logi NA > $ OrGCli: logi NA > $ OreFor: num -27 > $ GioFor: logi NA > $ OrGFor: logi NA > $ OreOrt: num -18 > $ GioOrt: logi NA > $ OrGOrt: logi NA > $ OreSpo: num -6 > $ GioSpo: logi NA > $ OrGSpo: logi NA > $ OreUff: num -7 > $ GioUff: logi NA > $ OrGUff: logi NA > $ temp : num 0 > > Is there any way I can format the numeric fields so that I get a > leading "+" whenever the value is > 0? In the specific case I would > need something like:Why? What do you want to do with it? If you want format numbers/strings you can use sprintf x<-rnorm(5) sprintf("%+f",x) but resulting values are eventually not numbers Regards Petr> > 'data.frame': 1 obs. of 20 variables: > $ Anno : chr "PREVISIONI VS TARGET" > $ OreTot: num +41 > $ GioTot: logi NA > $ OrGTot: logi NA > $ OreCli: num +99 > $ GioCli: logi NA > $ OrGCli: logi NA > $ OreFor: num -27 > $ GioFor: logi NA > $ OrGFor: logi NA > $ OreOrt: num -18 > $ GioOrt: logi NA > $ OrGOrt: logi NA > $ OreSpo: num -6 > $ GioSpo: logi NA > $ OrGSpo: logi NA > $ OreUff: num -7 > $ GioUff: logi NA > $ OrGUff: logi NA > $ temp : num 0 > > Thank you in advance, > > Luca > > Mr. Luca Meyer > www.lucameyer.com > R version 2.15.1 > Mac OS X 10.8 > > > > > > > > [[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, Try this: set.seed(1) dat1<-data.frame(OreTot=c(40,-7,41,35,7,15),GeoTot=c(TRUE,FALSE,TRUE,FALSE,TRUE,NA),OreCli=as.numeric(sample(1:25,6,replace=TRUE))) dat1$OreTot<-ifelse(dat1$OreTot>0,formatC(dat1$OreTot,format="f",digits=1,flag="+"),dat1$OreTot) dat1$OreCli<-ifelse(dat1$OreCli>0,formatC(dat1$OreCli,format="f",digits=1,flag="+"),dat1$OreCli) dat1 #OreTot GeoTot OreCli #1? +40.0?? TRUE?? +7.0 #2???? -7? FALSE? +10.0 #3? +41.0?? TRUE? +15.0 #4? +35.0? FALSE? +23.0 #5?? +7.0?? TRUE?? +6.0 #6? +15.0???? NA? +23.0 A.K. ----- Original Message ----- From: Luca Meyer <lucam1968 at gmail.com> To: R help <R-help at r-project.org> Cc: Sent: Thursday, August 30, 2012 1:54 AM Subject: [R] Leading plus in numeric fields Hello R experts, I have go this data frame: 'data.frame':??? 1 obs. of? 20 variables: $ Anno? : chr "PREVISIONI VS TARGET" $ OreTot: num 41 $ GioTot: logi NA $ OrGTot: logi NA $ OreCli: num 99 $ GioCli: logi NA $ OrGCli: logi NA $ OreFor: num -27 $ GioFor: logi NA $ OrGFor: logi NA $ OreOrt: num -18 $ GioOrt: logi NA $ OrGOrt: logi NA $ OreSpo: num -6 $ GioSpo: logi NA $ OrGSpo: logi NA $ OreUff: num -7 $ GioUff: logi NA $ OrGUff: logi NA $ temp? : num 0 Is there any way I can format the numeric fields so that I get a leading "+" whenever the value is > 0? In the specific case I would need something like: 'data.frame':??? 1 obs. of? 20 variables: $ Anno? : chr "PREVISIONI VS TARGET" $ OreTot: num +41 $ GioTot: logi NA $ OrGTot: logi NA $ OreCli: num +99 $ GioCli: logi NA $ OrGCli: logi NA $ OreFor: num -27 $ GioFor: logi NA $ OrGFor: logi NA $ OreOrt: num -18 $ GioOrt: logi NA $ OrGOrt: logi NA $ OreSpo: num -6 $ GioSpo: logi NA $ OrGSpo: logi NA $ OreUff: num -7 $ GioUff: logi NA $ OrGUff: logi NA $ temp? : num 0 Thank you in advance, Luca Mr. Luca Meyer www.lucameyer.com R version 2.15.1 Mac OS X 10.8 ??? [[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, You can also use "gsub" set.seed(1) dat1<-data.frame(OreTot=c(40,-7,41,35,7,15),GeoTot=c(TRUE,FALSE,TRUE,FALSE,TRUE,NA),OreCli=as.numeric(sample(1:25,6,replace=TRUE))) dat1[,1]<-ifelse(dat1[,1]>0,gsub("(\\d+)","+\\1",dat1[,1]),dat1[,1]) dat1[,3]<-ifelse(dat1[,3]>0,gsub("(\\d+)","+\\1",dat1[,3]),dat1[,3]) ?dat1 #? OreTot GeoTot OreCli #1??? +40?? TRUE???? +7 #2???? -7? FALSE??? +10 #3??? +41?? TRUE??? +15 #4??? +35? FALSE??? +23 #5???? +7?? TRUE???? +6 #6??? +15???? NA??? +23 ----- Original Message ----- From: Luca Meyer <lucam1968 at gmail.com> To: R help <R-help at r-project.org> Cc: Sent: Thursday, August 30, 2012 1:54 AM Subject: [R] Leading plus in numeric fields Hello R experts, I have go this data frame: 'data.frame':??? 1 obs. of? 20 variables: $ Anno? : chr "PREVISIONI VS TARGET" $ OreTot: num 41 $ GioTot: logi NA $ OrGTot: logi NA $ OreCli: num 99 $ GioCli: logi NA $ OrGCli: logi NA $ OreFor: num -27 $ GioFor: logi NA $ OrGFor: logi NA $ OreOrt: num -18 $ GioOrt: logi NA $ OrGOrt: logi NA $ OreSpo: num -6 $ GioSpo: logi NA $ OrGSpo: logi NA $ OreUff: num -7 $ GioUff: logi NA $ OrGUff: logi NA $ temp? : num 0 Is there any way I can format the numeric fields so that I get a leading "+" whenever the value is > 0? In the specific case I would need something like: 'data.frame':??? 1 obs. of? 20 variables: $ Anno? : chr "PREVISIONI VS TARGET" $ OreTot: num +41 $ GioTot: logi NA $ OrGTot: logi NA $ OreCli: num +99 $ GioCli: logi NA $ OrGCli: logi NA $ OreFor: num -27 $ GioFor: logi NA $ OrGFor: logi NA $ OreOrt: num -18 $ GioOrt: logi NA $ OrGOrt: logi NA $ OreSpo: num -6 $ GioSpo: logi NA $ OrGSpo: logi NA $ OreUff: num -7 $ GioUff: logi NA $ OrGUff: logi NA $ temp? : num 0 Thank you in advance, Luca Mr. Luca Meyer www.lucameyer.com R version 2.15.1 Mac OS X 10.8 ??? [[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, As you are more number of columns which are numeric, try this: set.seed(1) dat1<-data.frame(OreTot=c(40,-7,41,35,7,15),GeoTot=c(TRUE,FALSE,TRUE,FALSE,TRUE,NA),OreCli=as.numeric(sample(1:25,6,replace=TRUE))) dat2<-dat1[sapply(dat1,is.numeric)] dat3<-data.frame(sapply(dat2,function(x) ifelse(x>0,formatC(x,format="f",digits=0,flag="+"),x))) #or, dat4<-data.frame(sapply(dat2,function(x) ifelse(x>0,gsub("(\\d+)","+\\1",x),x))) dat4 ?# OreTot OreCli #1??? +40???? +7 #2???? -7??? +10 #3??? +41??? +15 #4??? +35??? +23 #5???? +7???? +6 #6??? +15??? +23 A.K. ----- Original Message ----- From: Luca Meyer <lucam1968 at gmail.com> To: R help <R-help at r-project.org> Cc: Sent: Thursday, August 30, 2012 1:54 AM Subject: [R] Leading plus in numeric fields Hello R experts, I have go this data frame: 'data.frame':??? 1 obs. of? 20 variables: $ Anno? : chr "PREVISIONI VS TARGET" $ OreTot: num 41 $ GioTot: logi NA $ OrGTot: logi NA $ OreCli: num 99 $ GioCli: logi NA $ OrGCli: logi NA $ OreFor: num -27 $ GioFor: logi NA $ OrGFor: logi NA $ OreOrt: num -18 $ GioOrt: logi NA $ OrGOrt: logi NA $ OreSpo: num -6 $ GioSpo: logi NA $ OrGSpo: logi NA $ OreUff: num -7 $ GioUff: logi NA $ OrGUff: logi NA $ temp? : num 0 Is there any way I can format the numeric fields so that I get a leading "+" whenever the value is > 0? In the specific case I would need something like: 'data.frame':??? 1 obs. of? 20 variables: $ Anno? : chr "PREVISIONI VS TARGET" $ OreTot: num +41 $ GioTot: logi NA $ OrGTot: logi NA $ OreCli: num +99 $ GioCli: logi NA $ OrGCli: logi NA $ OreFor: num -27 $ GioFor: logi NA $ OrGFor: logi NA $ OreOrt: num -18 $ GioOrt: logi NA $ OrGOrt: logi NA $ OreSpo: num -6 $ GioSpo: logi NA $ OrGSpo: logi NA $ OreUff: num -7 $ GioUff: logi NA $ OrGUff: logi NA $ temp? : num 0 Thank you in advance, Luca Mr. Luca Meyer www.lucameyer.com R version 2.15.1 Mac OS X 10.8 ??? [[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.