R-helper:
Below, code for generating a decile table. I am using the xtable package, but it is not quite right for the output. Issue #1. xtable inserts an unwanted column, before the first derived column DECILE Issue #2. In the last line "Total" I manually sum all columns, even though I only want the sums for second and third columns. If I calculate only second and third columns, the remaining columns would have NAs. Either scenario is not desired. Any suggestions, would be appreciated, for a package that addresses issue #1, and has an option for summing the desired two columns. Lastly, I read that one should rarely use "attach()", but if I don't the program will not run. An explanation of why I need attach() would also be appreciated. Thanks. Bruce **** Response <- data.frame(Response=rbinom(50,1,0.2), yhat=runif(50)) Response <- Response[order(Response$yhat,decreasing=TRUE),] Response[[2]] <- NULL cum_R <- cumsum(Response) sam_size <- nrow(Response) cum_n <- seq(1:1,sam_size) wt <- rep(c(1), times=sam_size) cum_wt <- cumsum(wt) dec <- (cum_n/sam_size) decc <- floor((cum_n*10)/(sam_size+1)) dec_mean <- aggregate(Response, by=list(decc), mean) dd_ <- data.frame(cum_R, sam_size, cum_wt, cum_n, decc) dd <- cbind(Response, dd_) names(dd)[2] <- "cum_R" dec_mean <- aggregate(Response ~ decc, dd, mean) wt <- rep(c(1), times=sam_size) cum_wt <- aggregate(wt ~ decc, dd, sum) cum_R <- aggregate(Response ~ decc, dd, sum) dec_mean_wt <- cbind(dec_mean, cum_wt) dec_mean_wt <- dec_mean_wt[-3] dec_mean_wt_R <- cbind(dec_mean_wt, cum_R) dec_mean_wt_R <- dec_mean_wt_R[-4] colnames(dec_mean_wt_R) <- c("Decile", "Resp_Rate", "No_Inds", "No_Resp") dec_mean_wt_R <- dec_mean_wt_R[,c(1,3,4,2)] cum_n <- dec_mean_wt_R[2] cum_n <- cumsum(cum_n) cum_R <- dec_mean_wt_R[3] cum_R <- cumsum(cum_R) dec_mean_wt_R_nR <- cbind(dec_mean_wt_R, cum_n, cum_R) colnames(dec_mean_wt_R_nR) <- c("Decile", "No_Inds", "No_Resp", "RespRate", "Cum_n", "Cum_R") dec_mean_wt_R_nR attach(dec_mean_wt_R_nR) Cum_RespRate <- (Cum_R/Cum_n)*100 options(digits=4) Decile_RespRate <- (No_Resp/No_Inds) dec_mean_wt_R_nRD <- cbind(dec_mean_wt_R_nR, Cum_RespRate, Decile_RespRate) avg_RR <- dec_mean_wt_R_nRD[10,7] Cum_Lift <- (Cum_RespRate/avg_RR)*100 DECILE <- c("top","2","3","4","5","6","7","8","9","bot") dec_mean_wt_R_nRDL <- cbind(DECILE, dec_mean_wt_R_nRD, Cum_Lift) dec_mean_wt_R_nRDL <- dec_mean_wt_R_nRDL[,c(1,3,4,9,8,10)] total_line<-cbind(DECILE="Total", as.data.frame(matrix(colSums(dec_mean_wt_R_nRDL[-1]),nrow=1))) names(total_line)<-names(dec_mean_wt_R_nRDL) dec_mean_wt_R_nRDLT<-rbind(dec_mean_wt_R_nRDL,total_line) decile_table <- dec_mean_wt_R_nRDLT decile_table #Install the xtable package: install.packages("xtable") #Load the xtable package: library(xtable) DECILE_TABLE <-xtable(decile_table) DECILE_TABLE print.xtable(DECILE_TABLE, type="html",file="C:/R_Data/DecileTable.html") **** -- ______________________________________________ [hidden email] 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. |
On 20/04/2017 1:09 PM, BR_email wrote:
> R-helper: > Below, code for generating a decile table. > I am using the xtable package, but it is not quite right for the output. > Issue #1. xtable inserts an unwanted column, before the first derived > column DECILE > Issue #2. In the last line "Total" I manually sum all columns, even > though I only want the sums for second and third columns. > If I calculate only second and third columns, the remaining columns > would have NAs. > Either scenario is not desired. > I haven't gone through your code yet, but the tables package is generally more flexible (though maybe harder to use) than xtable. I can't look at it now, but will try to remember to do so in a few hours if I don't see a better solution posted first. Duncan Murdoch ______________________________________________ [hidden email] 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. |
Duncan: Thanks. I've exhausted my search for a simple table package that also allows for column sums. If you are not familiar with the decile table, you will find it quite embedded with much insight for predominance of virtually any model.
Regards, Bruce ______________ Bruce Ratner PhD The Significant Statistician™ (516) 791-3544 Statistical Predictive Analytics -- www.DMSTAT1.com Machine-Learning Data Mining -- www.GenIQ.net > On Apr 20, 2017, at 3:06 PM, Duncan Murdoch <[hidden email]> wrote: > >> On 20/04/2017 1:09 PM, BR_email wrote: >> R-helper: >> Below, code for generating a decile table. >> I am using the xtable package, but it is not quite right for the output. >> Issue #1. xtable inserts an unwanted column, before the first derived >> column DECILE >> Issue #2. In the last line "Total" I manually sum all columns, even >> though I only want the sums for second and third columns. >> If I calculate only second and third columns, the remaining columns >> would have NAs. >> Either scenario is not desired. >> > > > I haven't gone through your code yet, but the tables package is generally more flexible (though maybe harder to use) than xtable. > > I can't look at it now, but will try to remember to do so in a few hours if I don't see a better solution posted first. > > Duncan Murdoch > > > ______________________________________________ [hidden email] 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. |
In reply to this post by Bruce Ratner PhD
#1 You can remove the rownames by adding the argument include.rownames=FALSE to print.xtable():
print.xtable(DECILE_TABLE, type="html",file="DecileTable.html", include.rownames=FALSE) #2 Prevent data.frame from converting the first column to a factor and use NAs for the columns where you don't want totals: dec_mean_wt_R_nRDL <- cbind(DECILE, dec_mean_wt_R_nRD, Cum_Lift, stringsAsFactors=FALSE) dec_mean_wt_R_nRDL <- dec_mean_wt_R_nRDL[,c(1,3,4,9,8,10)] total_line<-cbind(DECILE="Total", as.data.frame(matrix(c(colSums(dec_mean_wt_R_nRDL[ , 2:3]), rep(NA, 3)),nrow=1))) Now the table should print without totals in the last three columns and no rownames. attach is discouraged since it can lead to confusion when a variable name exists in the environment and in a data frame (or multiple data frames). It is easy to forget which version of the variable you are using. More typing, but less subject to confusion would be to use with(), eg: Cum_RespRate <- with(dec_mean_wt_R_nR, (Cum_R/Cum_n)*100) This way it is always clear where Cum_R and Cum_n are coming from. In your code cum_R = Cum_R and cum_n = Cum_n so you could also use Cum_RespRate <- cum_R/cum_n)*100 ------------------------------------- David L Carlson Department of Anthropology Texas A&M University College Station, TX 77840-4352 -----Original Message----- From: R-help [mailto:[hidden email]] On Behalf Of BR_email Sent: Thursday, April 20, 2017 12:10 PM To: [hidden email] Subject: [R] Looking for a package to replace xtable R-helper: Below, code for generating a decile table. I am using the xtable package, but it is not quite right for the output. Issue #1. xtable inserts an unwanted column, before the first derived column DECILE Issue #2. In the last line "Total" I manually sum all columns, even though I only want the sums for second and third columns. If I calculate only second and third columns, the remaining columns would have NAs. Either scenario is not desired. Any suggestions, would be appreciated, for a package that addresses issue #1, and has an option for summing the desired two columns. Lastly, I read that one should rarely use "attach()", but if I don't the program will not run. An explanation of why I need attach() would also be appreciated. Thanks. Bruce **** Response <- data.frame(Response=rbinom(50,1,0.2), yhat=runif(50)) Response <- Response[order(Response$yhat,decreasing=TRUE),] Response[[2]] <- NULL cum_R <- cumsum(Response) sam_size <- nrow(Response) cum_n <- seq(1:1,sam_size) wt <- rep(c(1), times=sam_size) cum_wt <- cumsum(wt) dec <- (cum_n/sam_size) decc <- floor((cum_n*10)/(sam_size+1)) dec_mean <- aggregate(Response, by=list(decc), mean) dd_ <- data.frame(cum_R, sam_size, cum_wt, cum_n, decc) dd <- cbind(Response, dd_) names(dd)[2] <- "cum_R" dec_mean <- aggregate(Response ~ decc, dd, mean) wt <- rep(c(1), times=sam_size) cum_wt <- aggregate(wt ~ decc, dd, sum) cum_R <- aggregate(Response ~ decc, dd, sum) dec_mean_wt <- cbind(dec_mean, cum_wt) dec_mean_wt <- dec_mean_wt[-3] dec_mean_wt_R <- cbind(dec_mean_wt, cum_R) dec_mean_wt_R <- dec_mean_wt_R[-4] colnames(dec_mean_wt_R) <- c("Decile", "Resp_Rate", "No_Inds", "No_Resp") dec_mean_wt_R <- dec_mean_wt_R[,c(1,3,4,2)] cum_n <- dec_mean_wt_R[2] cum_n <- cumsum(cum_n) cum_R <- dec_mean_wt_R[3] cum_R <- cumsum(cum_R) dec_mean_wt_R_nR <- cbind(dec_mean_wt_R, cum_n, cum_R) colnames(dec_mean_wt_R_nR) <- c("Decile", "No_Inds", "No_Resp", "RespRate", "Cum_n", "Cum_R") dec_mean_wt_R_nR attach(dec_mean_wt_R_nR) Cum_RespRate <- (Cum_R/Cum_n)*100 options(digits=4) Decile_RespRate <- (No_Resp/No_Inds) dec_mean_wt_R_nRD <- cbind(dec_mean_wt_R_nR, Cum_RespRate, Decile_RespRate) avg_RR <- dec_mean_wt_R_nRD[10,7] Cum_Lift <- (Cum_RespRate/avg_RR)*100 DECILE <- c("top","2","3","4","5","6","7","8","9","bot") dec_mean_wt_R_nRDL <- cbind(DECILE, dec_mean_wt_R_nRD, Cum_Lift) dec_mean_wt_R_nRDL <- dec_mean_wt_R_nRDL[,c(1,3,4,9,8,10)] total_line<-cbind(DECILE="Total", as.data.frame(matrix(colSums(dec_mean_wt_R_nRDL[-1]),nrow=1))) names(total_line)<-names(dec_mean_wt_R_nRDL) dec_mean_wt_R_nRDLT<-rbind(dec_mean_wt_R_nRDL,total_line) decile_table <- dec_mean_wt_R_nRDLT decile_table #Install the xtable package: install.packages("xtable") #Load the xtable package: library(xtable) DECILE_TABLE <-xtable(decile_table) DECILE_TABLE print.xtable(DECILE_TABLE, type="html",file="C:/R_Data/DecileTable.html") **** -- ______________________________________________ [hidden email] 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. ______________________________________________ [hidden email] 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. |
David:
Thanks so much. I will recode and let you know how it works out. Bruce ______________ Bruce Ratner PhD The Significant Statistician™ (516) 791-3544 Statistical Predictive Analytics -- www.DMSTAT1.com Machine-Learning Data Mining -- www.GenIQ.net > On Apr 20, 2017, at 4:31 PM, David L Carlson <[hidden email]> wrote: > > #1 You can remove the rownames by adding the argument include.rownames=FALSE to print.xtable(): > > print.xtable(DECILE_TABLE, type="html",file="DecileTable.html", include.rownames=FALSE) > > #2 Prevent data.frame from converting the first column to a factor and use NAs for the columns where you don't want totals: > > dec_mean_wt_R_nRDL <- cbind(DECILE, dec_mean_wt_R_nRD, Cum_Lift, stringsAsFactors=FALSE) > dec_mean_wt_R_nRDL <- dec_mean_wt_R_nRDL[,c(1,3,4,9,8,10)] > > total_line<-cbind(DECILE="Total", > as.data.frame(matrix(c(colSums(dec_mean_wt_R_nRDL[ , 2:3]), rep(NA, 3)),nrow=1))) > > Now the table should print without totals in the last three columns and no rownames. > > attach is discouraged since it can lead to confusion when a variable name exists in the environment and in a data frame (or multiple data frames). It is easy to forget which version of the variable you are using. More typing, but less subject to confusion would be to use with(), eg: > > Cum_RespRate <- with(dec_mean_wt_R_nR, (Cum_R/Cum_n)*100) > > This way it is always clear where Cum_R and Cum_n are coming from. In your code cum_R = Cum_R and cum_n = Cum_n so you could also use > > Cum_RespRate <- cum_R/cum_n)*100 > > ------------------------------------- > David L Carlson > Department of Anthropology > Texas A&M University > College Station, TX 77840-4352 > > -----Original Message----- > From: R-help [mailto:[hidden email]] On Behalf Of BR_email > Sent: Thursday, April 20, 2017 12:10 PM > To: [hidden email] > Subject: [R] Looking for a package to replace xtable > > R-helper: > Below, code for generating a decile table. > I am using the xtable package, but it is not quite right for the output. > Issue #1. xtable inserts an unwanted column, before the first derived > column DECILE > Issue #2. In the last line "Total" I manually sum all columns, even > though I only want the sums for second and third columns. > If I calculate only second and third columns, the remaining columns > would have NAs. > Either scenario is not desired. > > Any suggestions, would be appreciated, for a package that addresses > issue #1, > and has an option for summing the desired two columns. > > Lastly, I read that one should rarely use "attach()", but if I don't the > program will not run. > An explanation of why I need attach() would also be appreciated. > Thanks. > Bruce > > **** > Response <- data.frame(Response=rbinom(50,1,0.2), yhat=runif(50)) > Response <- Response[order(Response$yhat,decreasing=TRUE),] > > Response[[2]] <- NULL > > cum_R <- cumsum(Response) > sam_size <- nrow(Response) > > cum_n <- seq(1:1,sam_size) > wt <- rep(c(1), times=sam_size) > cum_wt <- cumsum(wt) > > dec <- (cum_n/sam_size) > decc <- floor((cum_n*10)/(sam_size+1)) > > dec_mean <- aggregate(Response, by=list(decc), mean) > > dd_ <- data.frame(cum_R, sam_size, cum_wt, cum_n, decc) > dd <- cbind(Response, dd_) > names(dd)[2] <- "cum_R" > > dec_mean <- aggregate(Response ~ decc, dd, mean) > > wt <- rep(c(1), times=sam_size) > cum_wt <- aggregate(wt ~ decc, dd, sum) > cum_R <- aggregate(Response ~ decc, dd, sum) > > dec_mean_wt <- cbind(dec_mean, cum_wt) > dec_mean_wt <- dec_mean_wt[-3] > > dec_mean_wt_R <- cbind(dec_mean_wt, cum_R) > dec_mean_wt_R <- dec_mean_wt_R[-4] > > colnames(dec_mean_wt_R) <- c("Decile", "Resp_Rate", "No_Inds", > "No_Resp") > > dec_mean_wt_R <- dec_mean_wt_R[,c(1,3,4,2)] > > cum_n <- dec_mean_wt_R[2] > cum_n <- cumsum(cum_n) > > cum_R <- dec_mean_wt_R[3] > cum_R <- cumsum(cum_R) > > dec_mean_wt_R_nR <- cbind(dec_mean_wt_R, cum_n, cum_R) > > colnames(dec_mean_wt_R_nR) <- > c("Decile", "No_Inds", "No_Resp", "RespRate", > "Cum_n", "Cum_R") > > dec_mean_wt_R_nR > > attach(dec_mean_wt_R_nR) > Cum_RespRate <- (Cum_R/Cum_n)*100 > > options(digits=4) > Decile_RespRate <- (No_Resp/No_Inds) > > dec_mean_wt_R_nRD <- cbind(dec_mean_wt_R_nR, Cum_RespRate, Decile_RespRate) > > avg_RR <- dec_mean_wt_R_nRD[10,7] > Cum_Lift <- (Cum_RespRate/avg_RR)*100 > > DECILE <- c("top","2","3","4","5","6","7","8","9","bot") > dec_mean_wt_R_nRDL <- cbind(DECILE, dec_mean_wt_R_nRD, Cum_Lift) > dec_mean_wt_R_nRDL <- dec_mean_wt_R_nRDL[,c(1,3,4,9,8,10)] > > total_line<-cbind(DECILE="Total", > as.data.frame(matrix(colSums(dec_mean_wt_R_nRDL[-1]),nrow=1))) > > names(total_line)<-names(dec_mean_wt_R_nRDL) > dec_mean_wt_R_nRDLT<-rbind(dec_mean_wt_R_nRDL,total_line) > decile_table <- dec_mean_wt_R_nRDLT > decile_table > > #Install the xtable package: install.packages("xtable") > #Load the xtable package: > library(xtable) > > DECILE_TABLE <-xtable(decile_table) > DECILE_TABLE > > print.xtable(DECILE_TABLE, type="html",file="C:/R_Data/DecileTable.html") > > **** > > -- > > ______________________________________________ > [hidden email] 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. > ______________________________________________ [hidden email] 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. |
In reply to this post by David Carlson
David:
All is perfect, almost - after I ran your corrections. Is there a way I can have more control of the column names, i.e., not be restricted to abbreviations headings, and center-justify? Thanks a lot, nice. Bruce David L Carlson wrote: > #1 You can remove the rownames by adding the argument include.rownames=FALSE to print.xtable(): > > print.xtable(DECILE_TABLE, type="html",file="DecileTable.html", include.rownames=FALSE) > > #2 Prevent data.frame from converting the first column to a factor and use NAs for the columns where you don't want totals: > > dec_mean_wt_R_nRDL <- cbind(DECILE, dec_mean_wt_R_nRD, Cum_Lift, stringsAsFactors=FALSE) > dec_mean_wt_R_nRDL <- dec_mean_wt_R_nRDL[,c(1,3,4,9,8,10)] > > total_line<-cbind(DECILE="Total", > as.data.frame(matrix(c(colSums(dec_mean_wt_R_nRDL[ , 2:3]), rep(NA, 3)),nrow=1))) > > Now the table should print without totals in the last three columns and no rownames. > > attach is discouraged since it can lead to confusion when a variable name exists in the environment and in a data frame (or multiple data frames). It is easy to forget which version of the variable you are using. More typing, but less subject to confusion would be to use with(), eg: > > Cum_RespRate <- with(dec_mean_wt_R_nR, (Cum_R/Cum_n)*100) > > This way it is always clear where Cum_R and Cum_n are coming from. In your code cum_R = Cum_R and cum_n = Cum_n so you could also use > > Cum_RespRate <- cum_R/cum_n)*100 > > ------------------------------------- > David L Carlson > Department of Anthropology > Texas A&M University > College Station, TX 77840-4352 > > -----Original Message----- > From: R-help [mailto:[hidden email]] On Behalf Of BR_email > Sent: Thursday, April 20, 2017 12:10 PM > To: [hidden email] > Subject: [R] Looking for a package to replace xtable > > R-helper: > Below, code for generating a decile table. > I am using the xtable package, but it is not quite right for the output. > Issue #1. xtable inserts an unwanted column, before the first derived > column DECILE > Issue #2. In the last line "Total" I manually sum all columns, even > though I only want the sums for second and third columns. > If I calculate only second and third columns, the remaining columns > would have NAs. > Either scenario is not desired. > > Any suggestions, would be appreciated, for a package that addresses > issue #1, > and has an option for summing the desired two columns. > > Lastly, I read that one should rarely use "attach()", but if I don't the > program will not run. > An explanation of why I need attach() would also be appreciated. > Thanks. > Bruce > > **** > Response <- data.frame(Response=rbinom(50,1,0.2), yhat=runif(50)) > Response <- Response[order(Response$yhat,decreasing=TRUE),] > > Response[[2]] <- NULL > > cum_R <- cumsum(Response) > sam_size <- nrow(Response) > > cum_n <- seq(1:1,sam_size) > wt <- rep(c(1), times=sam_size) > cum_wt <- cumsum(wt) > > dec <- (cum_n/sam_size) > decc <- floor((cum_n*10)/(sam_size+1)) > > dec_mean <- aggregate(Response, by=list(decc), mean) > > dd_ <- data.frame(cum_R, sam_size, cum_wt, cum_n, decc) > dd <- cbind(Response, dd_) > names(dd)[2] <- "cum_R" > > dec_mean <- aggregate(Response ~ decc, dd, mean) > > wt <- rep(c(1), times=sam_size) > cum_wt <- aggregate(wt ~ decc, dd, sum) > cum_R <- aggregate(Response ~ decc, dd, sum) > > dec_mean_wt <- cbind(dec_mean, cum_wt) > dec_mean_wt <- dec_mean_wt[-3] > > dec_mean_wt_R <- cbind(dec_mean_wt, cum_R) > dec_mean_wt_R <- dec_mean_wt_R[-4] > > colnames(dec_mean_wt_R) <- c("Decile", "Resp_Rate", "No_Inds", > "No_Resp") > > dec_mean_wt_R <- dec_mean_wt_R[,c(1,3,4,2)] > > cum_n <- dec_mean_wt_R[2] > cum_n <- cumsum(cum_n) > > cum_R <- dec_mean_wt_R[3] > cum_R <- cumsum(cum_R) > > dec_mean_wt_R_nR <- cbind(dec_mean_wt_R, cum_n, cum_R) > > colnames(dec_mean_wt_R_nR) <- > c("Decile", "No_Inds", "No_Resp", "RespRate", > "Cum_n", "Cum_R") > > dec_mean_wt_R_nR > > attach(dec_mean_wt_R_nR) > Cum_RespRate <- (Cum_R/Cum_n)*100 > > options(digits=4) > Decile_RespRate <- (No_Resp/No_Inds) > > dec_mean_wt_R_nRD <- cbind(dec_mean_wt_R_nR, Cum_RespRate, Decile_RespRate) > > avg_RR <- dec_mean_wt_R_nRD[10,7] > Cum_Lift <- (Cum_RespRate/avg_RR)*100 > > DECILE <- c("top","2","3","4","5","6","7","8","9","bot") > dec_mean_wt_R_nRDL <- cbind(DECILE, dec_mean_wt_R_nRD, Cum_Lift) > dec_mean_wt_R_nRDL <- dec_mean_wt_R_nRDL[,c(1,3,4,9,8,10)] > > total_line<-cbind(DECILE="Total", > as.data.frame(matrix(colSums(dec_mean_wt_R_nRDL[-1]),nrow=1))) > > names(total_line)<-names(dec_mean_wt_R_nRDL) > dec_mean_wt_R_nRDLT<-rbind(dec_mean_wt_R_nRDL,total_line) > decile_table <- dec_mean_wt_R_nRDLT > decile_table > > #Install the xtable package: install.packages("xtable") > #Load the xtable package: > library(xtable) > > DECILE_TABLE <-xtable(decile_table) > DECILE_TABLE > > print.xtable(DECILE_TABLE, type="html",file="C:/R_Data/DecileTable.html") > > **** > > -- > > ______________________________________________ > [hidden email] 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. > > > ______________________________________________ [hidden email] 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. |
Since you are generating html you can use html syntax.
You might also be interested in the ReportR package. -- Sent from my phone. Please excuse my brevity. On April 20, 2017 2:30:43 PM PDT, BR_email <[hidden email]> wrote: >David: >All is perfect, almost - after I ran your corrections. >Is there a way I can have more control of the column names, i.e., >not be restricted to abbreviations headings, and center-justify? > >Thanks a lot, nice. >Bruce > > > >David L Carlson wrote: >> #1 You can remove the rownames by adding the argument >include.rownames=FALSE to print.xtable(): >> >> print.xtable(DECILE_TABLE, type="html",file="DecileTable.html", >include.rownames=FALSE) >> >> #2 Prevent data.frame from converting the first column to a factor >and use NAs for the columns where you don't want totals: >> >> dec_mean_wt_R_nRDL <- cbind(DECILE, dec_mean_wt_R_nRD, Cum_Lift, >stringsAsFactors=FALSE) >> dec_mean_wt_R_nRDL <- dec_mean_wt_R_nRDL[,c(1,3,4,9,8,10)] >> >> total_line<-cbind(DECILE="Total", >> as.data.frame(matrix(c(colSums(dec_mean_wt_R_nRDL[ , 2:3]), >rep(NA, 3)),nrow=1))) >> >> Now the table should print without totals in the last three columns >and no rownames. >> >> attach is discouraged since it can lead to confusion when a variable >name exists in the environment and in a data frame (or multiple data >frames). It is easy to forget which version of the variable you are >using. More typing, but less subject to confusion would be to use >with(), eg: >> >> Cum_RespRate <- with(dec_mean_wt_R_nR, (Cum_R/Cum_n)*100) >> >> This way it is always clear where Cum_R and Cum_n are coming from. In >your code cum_R = Cum_R and cum_n = Cum_n so you could also use >> >> Cum_RespRate <- cum_R/cum_n)*100 >> >> ------------------------------------- >> David L Carlson >> Department of Anthropology >> Texas A&M University >> College Station, TX 77840-4352 >> >> -----Original Message----- >> From: R-help [mailto:[hidden email]] On Behalf Of >BR_email >> Sent: Thursday, April 20, 2017 12:10 PM >> To: [hidden email] >> Subject: [R] Looking for a package to replace xtable >> >> R-helper: >> Below, code for generating a decile table. >> I am using the xtable package, but it is not quite right for the >output. >> Issue #1. xtable inserts an unwanted column, before the first derived >> column DECILE >> Issue #2. In the last line "Total" I manually sum all columns, even >> though I only want the sums for second and third columns. >> If I calculate only second and third columns, the remaining columns >> would have NAs. >> Either scenario is not desired. >> >> Any suggestions, would be appreciated, for a package that addresses >> issue #1, >> and has an option for summing the desired two columns. >> >> Lastly, I read that one should rarely use "attach()", but if I don't >the >> program will not run. >> An explanation of why I need attach() would also be appreciated. >> Thanks. >> Bruce >> >> **** >> Response <- data.frame(Response=rbinom(50,1,0.2), yhat=runif(50)) >> Response <- Response[order(Response$yhat,decreasing=TRUE),] >> >> Response[[2]] <- NULL >> >> cum_R <- cumsum(Response) >> sam_size <- nrow(Response) >> >> cum_n <- seq(1:1,sam_size) >> wt <- rep(c(1), times=sam_size) >> cum_wt <- cumsum(wt) >> >> dec <- (cum_n/sam_size) >> decc <- floor((cum_n*10)/(sam_size+1)) >> >> dec_mean <- aggregate(Response, by=list(decc), mean) >> >> dd_ <- data.frame(cum_R, sam_size, cum_wt, cum_n, decc) >> dd <- cbind(Response, dd_) >> names(dd)[2] <- "cum_R" >> >> dec_mean <- aggregate(Response ~ decc, dd, mean) >> >> wt <- rep(c(1), times=sam_size) >> cum_wt <- aggregate(wt ~ decc, dd, sum) >> cum_R <- aggregate(Response ~ decc, dd, sum) >> >> dec_mean_wt <- cbind(dec_mean, cum_wt) >> dec_mean_wt <- dec_mean_wt[-3] >> >> dec_mean_wt_R <- cbind(dec_mean_wt, cum_R) >> dec_mean_wt_R <- dec_mean_wt_R[-4] >> >> colnames(dec_mean_wt_R) <- c("Decile", "Resp_Rate", "No_Inds", >> "No_Resp") >> >> dec_mean_wt_R <- dec_mean_wt_R[,c(1,3,4,2)] >> >> cum_n <- dec_mean_wt_R[2] >> cum_n <- cumsum(cum_n) >> >> cum_R <- dec_mean_wt_R[3] >> cum_R <- cumsum(cum_R) >> >> dec_mean_wt_R_nR <- cbind(dec_mean_wt_R, cum_n, cum_R) >> >> colnames(dec_mean_wt_R_nR) <- >> c("Decile", "No_Inds", "No_Resp", "RespRate", >> "Cum_n", "Cum_R") >> >> dec_mean_wt_R_nR >> >> attach(dec_mean_wt_R_nR) >> Cum_RespRate <- (Cum_R/Cum_n)*100 >> >> options(digits=4) >> Decile_RespRate <- (No_Resp/No_Inds) >> >> dec_mean_wt_R_nRD <- cbind(dec_mean_wt_R_nR, Cum_RespRate, >Decile_RespRate) >> >> avg_RR <- dec_mean_wt_R_nRD[10,7] >> Cum_Lift <- (Cum_RespRate/avg_RR)*100 >> >> DECILE <- c("top","2","3","4","5","6","7","8","9","bot") >> dec_mean_wt_R_nRDL <- cbind(DECILE, dec_mean_wt_R_nRD, Cum_Lift) >> dec_mean_wt_R_nRDL <- dec_mean_wt_R_nRDL[,c(1,3,4,9,8,10)] >> >> total_line<-cbind(DECILE="Total", >> as.data.frame(matrix(colSums(dec_mean_wt_R_nRDL[-1]),nrow=1))) >> >> names(total_line)<-names(dec_mean_wt_R_nRDL) >> dec_mean_wt_R_nRDLT<-rbind(dec_mean_wt_R_nRDL,total_line) >> decile_table <- dec_mean_wt_R_nRDLT >> decile_table >> >> #Install the xtable package: install.packages("xtable") >> #Load the xtable package: >> library(xtable) >> >> DECILE_TABLE <-xtable(decile_table) >> DECILE_TABLE >> >> print.xtable(DECILE_TABLE, >type="html",file="C:/R_Data/DecileTable.html") >> >> **** >> >> -- >> >> ______________________________________________ >> [hidden email] 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. >> >> >> > >______________________________________________ >[hidden email] 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. ______________________________________________ [hidden email] 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. |
Thanks, Jeff.
Bruce ______________ Bruce Ratner PhD The Significant Statistician™ (516) 791-3544 Statistical Predictive Analytics -- www.DMSTAT1.com Machine-Learning Data Mining -- www.GenIQ.net > On Apr 20, 2017, at 7:15 PM, Jeff Newmiller <[hidden email]> wrote: > > ReportR package ______________________________________________ [hidden email] 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. |
In reply to this post by Bruce Ratner PhD
You can rename the columns with colnames() before passing it to xtable. This will let you use characters that data.frame would automatically convert.
I don't see an easy way to center the columns when printing an html file. If you are not making too many of these, you could open the .html file into a WYSIWIG html editor such as BlueGriffon, make the changes and save the file. If you have Microsoft Excel and Word, another fallback solution is to read the .html file into Excel where you have a wide variety of styles. David C -----Original Message----- From: BR_email [mailto:[hidden email]] Sent: Thursday, April 20, 2017 4:31 PM To: David L Carlson <[hidden email]>; [hidden email] Subject: Re: [R] Looking for a package to replace xtable David: All is perfect, almost - after I ran your corrections. Is there a way I can have more control of the column names, i.e., not be restricted to abbreviations headings, and center-justify? Thanks a lot, nice. Bruce David L Carlson wrote: > #1 You can remove the rownames by adding the argument include.rownames=FALSE to print.xtable(): > > print.xtable(DECILE_TABLE, type="html",file="DecileTable.html", include.rownames=FALSE) > > #2 Prevent data.frame from converting the first column to a factor and use NAs for the columns where you don't want totals: > > dec_mean_wt_R_nRDL <- cbind(DECILE, dec_mean_wt_R_nRD, Cum_Lift, stringsAsFactors=FALSE) > dec_mean_wt_R_nRDL <- dec_mean_wt_R_nRDL[,c(1,3,4,9,8,10)] > > total_line<-cbind(DECILE="Total", > as.data.frame(matrix(c(colSums(dec_mean_wt_R_nRDL[ , 2:3]), rep(NA, 3)),nrow=1))) > > Now the table should print without totals in the last three columns and no rownames. > > attach is discouraged since it can lead to confusion when a variable name exists in the environment and in a data frame (or multiple data frames). It is easy to forget which version of the variable you are using. More typing, but less subject to confusion would be to use with(), eg: > > Cum_RespRate <- with(dec_mean_wt_R_nR, (Cum_R/Cum_n)*100) > > This way it is always clear where Cum_R and Cum_n are coming from. In your code cum_R = Cum_R and cum_n = Cum_n so you could also use > > Cum_RespRate <- cum_R/cum_n)*100 > > ------------------------------------- > David L Carlson > Department of Anthropology > Texas A&M University > College Station, TX 77840-4352 > > -----Original Message----- > From: R-help [mailto:[hidden email]] On Behalf Of BR_email > Sent: Thursday, April 20, 2017 12:10 PM > To: [hidden email] > Subject: [R] Looking for a package to replace xtable > > R-helper: > Below, code for generating a decile table. > I am using the xtable package, but it is not quite right for the output. > Issue #1. xtable inserts an unwanted column, before the first derived > column DECILE > Issue #2. In the last line "Total" I manually sum all columns, even > though I only want the sums for second and third columns. > If I calculate only second and third columns, the remaining columns > would have NAs. > Either scenario is not desired. > > Any suggestions, would be appreciated, for a package that addresses > issue #1, > and has an option for summing the desired two columns. > > Lastly, I read that one should rarely use "attach()", but if I don't the > program will not run. > An explanation of why I need attach() would also be appreciated. > Thanks. > Bruce > > **** > Response <- data.frame(Response=rbinom(50,1,0.2), yhat=runif(50)) > Response <- Response[order(Response$yhat,decreasing=TRUE),] > > Response[[2]] <- NULL > > cum_R <- cumsum(Response) > sam_size <- nrow(Response) > > cum_n <- seq(1:1,sam_size) > wt <- rep(c(1), times=sam_size) > cum_wt <- cumsum(wt) > > dec <- (cum_n/sam_size) > decc <- floor((cum_n*10)/(sam_size+1)) > > dec_mean <- aggregate(Response, by=list(decc), mean) > > dd_ <- data.frame(cum_R, sam_size, cum_wt, cum_n, decc) > dd <- cbind(Response, dd_) > names(dd)[2] <- "cum_R" > > dec_mean <- aggregate(Response ~ decc, dd, mean) > > wt <- rep(c(1), times=sam_size) > cum_wt <- aggregate(wt ~ decc, dd, sum) > cum_R <- aggregate(Response ~ decc, dd, sum) > > dec_mean_wt <- cbind(dec_mean, cum_wt) > dec_mean_wt <- dec_mean_wt[-3] > > dec_mean_wt_R <- cbind(dec_mean_wt, cum_R) > dec_mean_wt_R <- dec_mean_wt_R[-4] > > colnames(dec_mean_wt_R) <- c("Decile", "Resp_Rate", "No_Inds", > "No_Resp") > > dec_mean_wt_R <- dec_mean_wt_R[,c(1,3,4,2)] > > cum_n <- dec_mean_wt_R[2] > cum_n <- cumsum(cum_n) > > cum_R <- dec_mean_wt_R[3] > cum_R <- cumsum(cum_R) > > dec_mean_wt_R_nR <- cbind(dec_mean_wt_R, cum_n, cum_R) > > colnames(dec_mean_wt_R_nR) <- > c("Decile", "No_Inds", "No_Resp", "RespRate", > "Cum_n", "Cum_R") > > dec_mean_wt_R_nR > > attach(dec_mean_wt_R_nR) > Cum_RespRate <- (Cum_R/Cum_n)*100 > > options(digits=4) > Decile_RespRate <- (No_Resp/No_Inds) > > dec_mean_wt_R_nRD <- cbind(dec_mean_wt_R_nR, Cum_RespRate, Decile_RespRate) > > avg_RR <- dec_mean_wt_R_nRD[10,7] > Cum_Lift <- (Cum_RespRate/avg_RR)*100 > > DECILE <- c("top","2","3","4","5","6","7","8","9","bot") > dec_mean_wt_R_nRDL <- cbind(DECILE, dec_mean_wt_R_nRD, Cum_Lift) > dec_mean_wt_R_nRDL <- dec_mean_wt_R_nRDL[,c(1,3,4,9,8,10)] > > total_line<-cbind(DECILE="Total", > as.data.frame(matrix(colSums(dec_mean_wt_R_nRDL[-1]),nrow=1))) > > names(total_line)<-names(dec_mean_wt_R_nRDL) > dec_mean_wt_R_nRDLT<-rbind(dec_mean_wt_R_nRDL,total_line) > decile_table <- dec_mean_wt_R_nRDLT > decile_table > > #Install the xtable package: install.packages("xtable") > #Load the xtable package: > library(xtable) > > DECILE_TABLE <-xtable(decile_table) > DECILE_TABLE > > print.xtable(DECILE_TABLE, type="html",file="C:/R_Data/DecileTable.html") > > **** > > -- > > ______________________________________________ > [hidden email] 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. > > > ______________________________________________ [hidden email] 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. |
David:
Got it! Thanks, again. Bruce ______________ Bruce Ratner PhD The Significant Statistician™ (516) 791-3544 Statistical Predictive Analytics -- www.DMSTAT1.com Machine-Learning Data Mining -- www.GenIQ.net > On Apr 21, 2017, at 10:56 AM, David L Carlson <[hidden email]> wrote: > > You can rename the columns with colnames() before passing it to xtable. This will let you use characters that data.frame would automatically convert. > > I don't see an easy way to center the columns when printing an html file. If you are not making too many of these, you could open the .html file into a WYSIWIG html editor such as BlueGriffon, make the changes and save the file. If you have Microsoft Excel and Word, another fallback solution is to read the .html file into Excel where you have a wide variety of styles. > > David C > > -----Original Message----- > From: BR_email [mailto:[hidden email]] > Sent: Thursday, April 20, 2017 4:31 PM > To: David L Carlson <[hidden email]>; [hidden email] > Subject: Re: [R] Looking for a package to replace xtable > > David: > All is perfect, almost - after I ran your corrections. > Is there a way I can have more control of the column names, i.e., > not be restricted to abbreviations headings, and center-justify? > > Thanks a lot, nice. > Bruce > > > > David L Carlson wrote: >> #1 You can remove the rownames by adding the argument include.rownames=FALSE to print.xtable(): >> >> print.xtable(DECILE_TABLE, type="html",file="DecileTable.html", include.rownames=FALSE) >> >> #2 Prevent data.frame from converting the first column to a factor and use NAs for the columns where you don't want totals: >> >> dec_mean_wt_R_nRDL <- cbind(DECILE, dec_mean_wt_R_nRD, Cum_Lift, stringsAsFactors=FALSE) >> dec_mean_wt_R_nRDL <- dec_mean_wt_R_nRDL[,c(1,3,4,9,8,10)] >> >> total_line<-cbind(DECILE="Total", >> as.data.frame(matrix(c(colSums(dec_mean_wt_R_nRDL[ , 2:3]), rep(NA, 3)),nrow=1))) >> >> Now the table should print without totals in the last three columns and no rownames. >> >> attach is discouraged since it can lead to confusion when a variable name exists in the environment and in a data frame (or multiple data frames). It is easy to forget which version of the variable you are using. More typing, but less subject to confusion would be to use with(), eg: >> >> Cum_RespRate <- with(dec_mean_wt_R_nR, (Cum_R/Cum_n)*100) >> >> This way it is always clear where Cum_R and Cum_n are coming from. In your code cum_R = Cum_R and cum_n = Cum_n so you could also use >> >> Cum_RespRate <- cum_R/cum_n)*100 >> >> ------------------------------------- >> David L Carlson >> Department of Anthropology >> Texas A&M University >> College Station, TX 77840-4352 >> >> -----Original Message----- >> From: R-help [mailto:[hidden email]] On Behalf Of BR_email >> Sent: Thursday, April 20, 2017 12:10 PM >> To: [hidden email] >> Subject: [R] Looking for a package to replace xtable >> >> R-helper: >> Below, code for generating a decile table. >> I am using the xtable package, but it is not quite right for the output. >> Issue #1. xtable inserts an unwanted column, before the first derived >> column DECILE >> Issue #2. In the last line "Total" I manually sum all columns, even >> though I only want the sums for second and third columns. >> If I calculate only second and third columns, the remaining columns >> would have NAs. >> Either scenario is not desired. >> >> Any suggestions, would be appreciated, for a package that addresses >> issue #1, >> and has an option for summing the desired two columns. >> >> Lastly, I read that one should rarely use "attach()", but if I don't the >> program will not run. >> An explanation of why I need attach() would also be appreciated. >> Thanks. >> Bruce >> >> **** >> Response <- data.frame(Response=rbinom(50,1,0.2), yhat=runif(50)) >> Response <- Response[order(Response$yhat,decreasing=TRUE),] >> >> Response[[2]] <- NULL >> >> cum_R <- cumsum(Response) >> sam_size <- nrow(Response) >> >> cum_n <- seq(1:1,sam_size) >> wt <- rep(c(1), times=sam_size) >> cum_wt <- cumsum(wt) >> >> dec <- (cum_n/sam_size) >> decc <- floor((cum_n*10)/(sam_size+1)) >> >> dec_mean <- aggregate(Response, by=list(decc), mean) >> >> dd_ <- data.frame(cum_R, sam_size, cum_wt, cum_n, decc) >> dd <- cbind(Response, dd_) >> names(dd)[2] <- "cum_R" >> >> dec_mean <- aggregate(Response ~ decc, dd, mean) >> >> wt <- rep(c(1), times=sam_size) >> cum_wt <- aggregate(wt ~ decc, dd, sum) >> cum_R <- aggregate(Response ~ decc, dd, sum) >> >> dec_mean_wt <- cbind(dec_mean, cum_wt) >> dec_mean_wt <- dec_mean_wt[-3] >> >> dec_mean_wt_R <- cbind(dec_mean_wt, cum_R) >> dec_mean_wt_R <- dec_mean_wt_R[-4] >> >> colnames(dec_mean_wt_R) <- c("Decile", "Resp_Rate", "No_Inds", >> "No_Resp") >> >> dec_mean_wt_R <- dec_mean_wt_R[,c(1,3,4,2)] >> >> cum_n <- dec_mean_wt_R[2] >> cum_n <- cumsum(cum_n) >> >> cum_R <- dec_mean_wt_R[3] >> cum_R <- cumsum(cum_R) >> >> dec_mean_wt_R_nR <- cbind(dec_mean_wt_R, cum_n, cum_R) >> >> colnames(dec_mean_wt_R_nR) <- >> c("Decile", "No_Inds", "No_Resp", "RespRate", >> "Cum_n", "Cum_R") >> >> dec_mean_wt_R_nR >> >> attach(dec_mean_wt_R_nR) >> Cum_RespRate <- (Cum_R/Cum_n)*100 >> >> options(digits=4) >> Decile_RespRate <- (No_Resp/No_Inds) >> >> dec_mean_wt_R_nRD <- cbind(dec_mean_wt_R_nR, Cum_RespRate, Decile_RespRate) >> >> avg_RR <- dec_mean_wt_R_nRD[10,7] >> Cum_Lift <- (Cum_RespRate/avg_RR)*100 >> >> DECILE <- c("top","2","3","4","5","6","7","8","9","bot") >> dec_mean_wt_R_nRDL <- cbind(DECILE, dec_mean_wt_R_nRD, Cum_Lift) >> dec_mean_wt_R_nRDL <- dec_mean_wt_R_nRDL[,c(1,3,4,9,8,10)] >> >> total_line<-cbind(DECILE="Total", >> as.data.frame(matrix(colSums(dec_mean_wt_R_nRDL[-1]),nrow=1))) >> >> names(total_line)<-names(dec_mean_wt_R_nRDL) >> dec_mean_wt_R_nRDLT<-rbind(dec_mean_wt_R_nRDL,total_line) >> decile_table <- dec_mean_wt_R_nRDLT >> decile_table >> >> #Install the xtable package: install.packages("xtable") >> #Load the xtable package: >> library(xtable) >> >> DECILE_TABLE <-xtable(decile_table) >> DECILE_TABLE >> >> print.xtable(DECILE_TABLE, type="html",file="C:/R_Data/DecileTable.html") >> >> **** >> >> -- >> >> ______________________________________________ >> [hidden email] 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. >> >> >> > > ______________________________________________ [hidden email] 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. |
In reply to this post by David Carlson
David:
Hate to bother you, but because you have seen my code perhaps you can tell me what I am doing wrong. I want to replicate my original Response data by 0 times, called ResponseX10. All is good until the first calculation, cum_R. Would you kindly, assist me? Thanks. Bruce **** Response <- data.frame(Response=rbinom(50,1,0.2), yhat=runif(50)) Response <- Response[order(Response$yhat,decreasing=TRUE),] ResponseX10 <- do.call(rbind, replicate(10, Response, simplify=FALSE)) ResponseX10 <- ResponseX10[order(ResponseX10$yhat,decreasing=TRUE),] ResponseX10[[2]] <- NULL ResponseX10 cum_R <- cumsum(Response) cum_R ******* Bruce Ratner, Ph.D. The Significant Statistician™ (516) 791-3544 Statistical Predictive Analtyics -- www.DMSTAT1.com Machine-Learning Data Mining and Modeling -- www.GenIQ.net David L Carlson wrote: > You can rename the columns with colnames() before passing it to xtable. This will let you use characters that data.frame would automatically convert. > > I don't see an easy way to center the columns when printing an html file. If you are not making too many of these, you could open the .html file into a WYSIWIG html editor such as BlueGriffon, make the changes and save the file. If you have Microsoft Excel and Word, another fallback solution is to read the .html file into Excel where you have a wide variety of styles. > > David C > > -----Original Message----- > From: BR_email [mailto:[hidden email]] > Sent: Thursday, April 20, 2017 4:31 PM > To: David L Carlson <[hidden email]>; [hidden email] > Subject: Re: [R] Looking for a package to replace xtable > > David: > All is perfect, almost - after I ran your corrections. > Is there a way I can have more control of the column names, i.e., > not be restricted to abbreviations headings, and center-justify? > > Thanks a lot, nice. > Bruce > > > > David L Carlson wrote: >> #1 You can remove the rownames by adding the argument include.rownames=FALSE to print.xtable(): >> >> print.xtable(DECILE_TABLE, type="html",file="DecileTable.html", include.rownames=FALSE) >> >> #2 Prevent data.frame from converting the first column to a factor and use NAs for the columns where you don't want totals: >> >> dec_mean_wt_R_nRDL <- cbind(DECILE, dec_mean_wt_R_nRD, Cum_Lift, stringsAsFactors=FALSE) >> dec_mean_wt_R_nRDL <- dec_mean_wt_R_nRDL[,c(1,3,4,9,8,10)] >> >> total_line<-cbind(DECILE="Total", >> as.data.frame(matrix(c(colSums(dec_mean_wt_R_nRDL[ , 2:3]), rep(NA, 3)),nrow=1))) >> >> Now the table should print without totals in the last three columns and no rownames. >> >> attach is discouraged since it can lead to confusion when a variable name exists in the environment and in a data frame (or multiple data frames). It is easy to forget which version of the variable you are using. More typing, but less subject to confusion would be to use with(), eg: >> >> Cum_RespRate <- with(dec_mean_wt_R_nR, (Cum_R/Cum_n)*100) >> >> This way it is always clear where Cum_R and Cum_n are coming from. In your code cum_R = Cum_R and cum_n = Cum_n so you could also use >> >> Cum_RespRate <- cum_R/cum_n)*100 >> >> ------------------------------------- >> David L Carlson >> Department of Anthropology >> Texas A&M University >> College Station, TX 77840-4352 >> >> -----Original Message----- >> From: R-help [mailto:[hidden email]] On Behalf Of BR_email >> Sent: Thursday, April 20, 2017 12:10 PM >> To: [hidden email] >> Subject: [R] Looking for a package to replace xtable >> >> R-helper: >> Below, code for generating a decile table. >> I am using the xtable package, but it is not quite right for the output. >> Issue #1. xtable inserts an unwanted column, before the first derived >> column DECILE >> Issue #2. In the last line "Total" I manually sum all columns, even >> though I only want the sums for second and third columns. >> If I calculate only second and third columns, the remaining columns >> would have NAs. >> Either scenario is not desired. >> >> Any suggestions, would be appreciated, for a package that addresses >> issue #1, >> and has an option for summing the desired two columns. >> >> Lastly, I read that one should rarely use "attach()", but if I don't the >> program will not run. >> An explanation of why I need attach() would also be appreciated. >> Thanks. >> Bruce >> >> **** >> Response <- data.frame(Response=rbinom(50,1,0.2), yhat=runif(50)) >> Response <- Response[order(Response$yhat,decreasing=TRUE),] >> >> Response[[2]] <- NULL >> >> cum_R <- cumsum(Response) >> sam_size <- nrow(Response) >> >> cum_n <- seq(1:1,sam_size) >> wt <- rep(c(1), times=sam_size) >> cum_wt <- cumsum(wt) >> >> dec <- (cum_n/sam_size) >> decc <- floor((cum_n*10)/(sam_size+1)) >> >> dec_mean <- aggregate(Response, by=list(decc), mean) >> >> dd_ <- data.frame(cum_R, sam_size, cum_wt, cum_n, decc) >> dd <- cbind(Response, dd_) >> names(dd)[2] <- "cum_R" >> >> dec_mean <- aggregate(Response ~ decc, dd, mean) >> >> wt <- rep(c(1), times=sam_size) >> cum_wt <- aggregate(wt ~ decc, dd, sum) >> cum_R <- aggregate(Response ~ decc, dd, sum) >> >> dec_mean_wt <- cbind(dec_mean, cum_wt) >> dec_mean_wt <- dec_mean_wt[-3] >> >> dec_mean_wt_R <- cbind(dec_mean_wt, cum_R) >> dec_mean_wt_R <- dec_mean_wt_R[-4] >> >> colnames(dec_mean_wt_R) <- c("Decile", "Resp_Rate", "No_Inds", >> "No_Resp") >> >> dec_mean_wt_R <- dec_mean_wt_R[,c(1,3,4,2)] >> >> cum_n <- dec_mean_wt_R[2] >> cum_n <- cumsum(cum_n) >> >> cum_R <- dec_mean_wt_R[3] >> cum_R <- cumsum(cum_R) >> >> dec_mean_wt_R_nR <- cbind(dec_mean_wt_R, cum_n, cum_R) >> >> colnames(dec_mean_wt_R_nR) <- >> c("Decile", "No_Inds", "No_Resp", "RespRate", >> "Cum_n", "Cum_R") >> >> dec_mean_wt_R_nR >> >> attach(dec_mean_wt_R_nR) >> Cum_RespRate <- (Cum_R/Cum_n)*100 >> >> options(digits=4) >> Decile_RespRate <- (No_Resp/No_Inds) >> >> dec_mean_wt_R_nRD <- cbind(dec_mean_wt_R_nR, Cum_RespRate, Decile_RespRate) >> >> avg_RR <- dec_mean_wt_R_nRD[10,7] >> Cum_Lift <- (Cum_RespRate/avg_RR)*100 >> >> DECILE <- c("top","2","3","4","5","6","7","8","9","bot") >> dec_mean_wt_R_nRDL <- cbind(DECILE, dec_mean_wt_R_nRD, Cum_Lift) >> dec_mean_wt_R_nRDL <- dec_mean_wt_R_nRDL[,c(1,3,4,9,8,10)] >> >> total_line<-cbind(DECILE="Total", >> as.data.frame(matrix(colSums(dec_mean_wt_R_nRDL[-1]),nrow=1))) >> >> names(total_line)<-names(dec_mean_wt_R_nRDL) >> dec_mean_wt_R_nRDLT<-rbind(dec_mean_wt_R_nRDL,total_line) >> decile_table <- dec_mean_wt_R_nRDLT >> decile_table >> >> #Install the xtable package: install.packages("xtable") >> #Load the xtable package: >> library(xtable) >> >> DECILE_TABLE <-xtable(decile_table) >> DECILE_TABLE >> >> print.xtable(DECILE_TABLE, type="html",file="C:/R_Data/DecileTable.html") >> >> **** >> >> -- >> >> ______________________________________________ >> [hidden email] 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. >> >> >> > > > ______________________________________________ [hidden email] 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. |
??
Works for me. Perhaps show us the output of sessionInfo(). Mine is: R version 3.3.3 (2017-03-06) Platform: x86_64-apple-darwin13.4.0 (64-bit) Running under: macOS Sierra 10.12.3 locale: [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8 attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] lattice_0.20-34 backports_1.0.5 loaded via a namespace (and not attached): [1] tools_3.3.3 grid_3.3.3 --Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Fri, Apr 21, 2017 at 10:14 AM, BR_email <[hidden email]> wrote: > David: > Hate to bother you, but because you have seen my code perhaps > you can tell me what I am doing wrong. > I want to replicate my original Response data by 0 times, called > ResponseX10. > All is good until the first calculation, cum_R. > Would you kindly, assist me? > Thanks. > Bruce > **** > Response <- data.frame(Response=rbinom(50,1,0.2), yhat=runif(50)) > Response <- Response[order(Response$yhat,decreasing=TRUE),] > > ResponseX10 <- do.call(rbind, replicate(10, Response, simplify=FALSE)) > > ResponseX10 <- ResponseX10[order(ResponseX10$yhat,decreasing=TRUE),] > > ResponseX10[[2]] <- NULL > ResponseX10 > > cum_R <- cumsum(Response) > cum_R > ******* > > Bruce Ratner, Ph.D. > The Significant Statistician™ > (516) 791-3544 > Statistical Predictive Analtyics -- www.DMSTAT1.com > Machine-Learning Data Mining and Modeling -- www.GenIQ.net > > David L Carlson wrote: >> >> You can rename the columns with colnames() before passing it to xtable. >> This will let you use characters that data.frame would automatically >> convert. >> >> I don't see an easy way to center the columns when printing an html file. >> If you are not making too many of these, you could open the .html file into >> a WYSIWIG html editor such as BlueGriffon, make the changes and save the >> file. If you have Microsoft Excel and Word, another fallback solution is to >> read the .html file into Excel where you have a wide variety of styles. >> >> David C >> >> -----Original Message----- >> From: BR_email [mailto:[hidden email]] >> Sent: Thursday, April 20, 2017 4:31 PM >> To: David L Carlson <[hidden email]>; [hidden email] >> Subject: Re: [R] Looking for a package to replace xtable >> >> David: >> All is perfect, almost - after I ran your corrections. >> Is there a way I can have more control of the column names, i.e., >> not be restricted to abbreviations headings, and center-justify? >> >> Thanks a lot, nice. >> Bruce >> >> >> David L Carlson wrote: >>> >>> #1 You can remove the rownames by adding the argument >>> include.rownames=FALSE to print.xtable(): >>> >>> print.xtable(DECILE_TABLE, type="html",file="DecileTable.html", >>> include.rownames=FALSE) >>> >>> #2 Prevent data.frame from converting the first column to a factor and >>> use NAs for the columns where you don't want totals: >>> >>> dec_mean_wt_R_nRDL <- cbind(DECILE, dec_mean_wt_R_nRD, Cum_Lift, >>> stringsAsFactors=FALSE) >>> dec_mean_wt_R_nRDL <- dec_mean_wt_R_nRDL[,c(1,3,4,9,8,10)] >>> >>> total_line<-cbind(DECILE="Total", >>> as.data.frame(matrix(c(colSums(dec_mean_wt_R_nRDL[ , 2:3]), rep(NA, >>> 3)),nrow=1))) >>> >>> Now the table should print without totals in the last three columns and >>> no rownames. >>> >>> attach is discouraged since it can lead to confusion when a variable name >>> exists in the environment and in a data frame (or multiple data frames). It >>> is easy to forget which version of the variable you are using. More typing, >>> but less subject to confusion would be to use with(), eg: >>> >>> Cum_RespRate <- with(dec_mean_wt_R_nR, (Cum_R/Cum_n)*100) >>> >>> This way it is always clear where Cum_R and Cum_n are coming from. In >>> your code cum_R = Cum_R and cum_n = Cum_n so you could also use >>> >>> Cum_RespRate <- cum_R/cum_n)*100 >>> >>> ------------------------------------- >>> David L Carlson >>> Department of Anthropology >>> Texas A&M University >>> College Station, TX 77840-4352 >>> >>> -----Original Message----- >>> From: R-help [mailto:[hidden email]] On Behalf Of BR_email >>> Sent: Thursday, April 20, 2017 12:10 PM >>> To: [hidden email] >>> Subject: [R] Looking for a package to replace xtable >>> >>> R-helper: >>> Below, code for generating a decile table. >>> I am using the xtable package, but it is not quite right for the output. >>> Issue #1. xtable inserts an unwanted column, before the first derived >>> column DECILE >>> Issue #2. In the last line "Total" I manually sum all columns, even >>> though I only want the sums for second and third columns. >>> If I calculate only second and third columns, the remaining columns >>> would have NAs. >>> Either scenario is not desired. >>> >>> Any suggestions, would be appreciated, for a package that addresses >>> issue #1, >>> and has an option for summing the desired two columns. >>> >>> Lastly, I read that one should rarely use "attach()", but if I don't the >>> program will not run. >>> An explanation of why I need attach() would also be appreciated. >>> Thanks. >>> Bruce >>> >>> **** >>> Response <- data.frame(Response=rbinom(50,1,0.2), yhat=runif(50)) >>> Response <- Response[order(Response$yhat,decreasing=TRUE),] >>> >>> Response[[2]] <- NULL >>> >>> cum_R <- cumsum(Response) >>> sam_size <- nrow(Response) >>> >>> cum_n <- seq(1:1,sam_size) >>> wt <- rep(c(1), times=sam_size) >>> cum_wt <- cumsum(wt) >>> >>> dec <- (cum_n/sam_size) >>> decc <- floor((cum_n*10)/(sam_size+1)) >>> >>> dec_mean <- aggregate(Response, by=list(decc), mean) >>> >>> dd_ <- data.frame(cum_R, sam_size, cum_wt, cum_n, decc) >>> dd <- cbind(Response, dd_) >>> names(dd)[2] <- "cum_R" >>> >>> dec_mean <- aggregate(Response ~ decc, dd, mean) >>> >>> wt <- rep(c(1), times=sam_size) >>> cum_wt <- aggregate(wt ~ decc, dd, sum) >>> cum_R <- aggregate(Response ~ decc, dd, sum) >>> >>> dec_mean_wt <- cbind(dec_mean, cum_wt) >>> dec_mean_wt <- dec_mean_wt[-3] >>> >>> dec_mean_wt_R <- cbind(dec_mean_wt, cum_R) >>> dec_mean_wt_R <- dec_mean_wt_R[-4] >>> >>> colnames(dec_mean_wt_R) <- c("Decile", "Resp_Rate", "No_Inds", >>> "No_Resp") >>> >>> dec_mean_wt_R <- dec_mean_wt_R[,c(1,3,4,2)] >>> >>> cum_n <- dec_mean_wt_R[2] >>> cum_n <- cumsum(cum_n) >>> >>> cum_R <- dec_mean_wt_R[3] >>> cum_R <- cumsum(cum_R) >>> >>> dec_mean_wt_R_nR <- cbind(dec_mean_wt_R, cum_n, cum_R) >>> >>> colnames(dec_mean_wt_R_nR) <- >>> c("Decile", "No_Inds", "No_Resp", "RespRate", >>> "Cum_n", "Cum_R") >>> >>> dec_mean_wt_R_nR >>> >>> attach(dec_mean_wt_R_nR) >>> Cum_RespRate <- (Cum_R/Cum_n)*100 >>> >>> options(digits=4) >>> Decile_RespRate <- (No_Resp/No_Inds) >>> >>> dec_mean_wt_R_nRD <- cbind(dec_mean_wt_R_nR, Cum_RespRate, >>> Decile_RespRate) >>> >>> avg_RR <- dec_mean_wt_R_nRD[10,7] >>> Cum_Lift <- (Cum_RespRate/avg_RR)*100 >>> >>> DECILE <- c("top","2","3","4","5","6","7","8","9","bot") >>> dec_mean_wt_R_nRDL <- cbind(DECILE, dec_mean_wt_R_nRD, Cum_Lift) >>> dec_mean_wt_R_nRDL <- dec_mean_wt_R_nRDL[,c(1,3,4,9,8,10)] >>> >>> total_line<-cbind(DECILE="Total", >>> as.data.frame(matrix(colSums(dec_mean_wt_R_nRDL[-1]),nrow=1))) >>> >>> names(total_line)<-names(dec_mean_wt_R_nRDL) >>> dec_mean_wt_R_nRDLT<-rbind(dec_mean_wt_R_nRDL,total_line) >>> decile_table <- dec_mean_wt_R_nRDLT >>> decile_table >>> >>> #Install the xtable package: install.packages("xtable") >>> #Load the xtable package: >>> library(xtable) >>> >>> DECILE_TABLE <-xtable(decile_table) >>> DECILE_TABLE >>> >>> print.xtable(DECILE_TABLE, type="html",file="C:/R_Data/DecileTable.html") >>> >>> **** >>> >>> -- >>> >>> ______________________________________________ >>> [hidden email] 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. >>> >>> >>> >> >> >> > > ______________________________________________ > [hidden email] 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. ______________________________________________ [hidden email] 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. |
In reply to this post by Bruce Ratner PhD
David:
I tried somethings and got a little more working. Now, I am struck at last line provided: "dec_mean <- aggregate(Response ~ decc, dd, mean)" Any help is appreciated. Bruce ***** Resp <- data.frame(Response=rbinom(50,1,0.2), yhat=runif(50)) Resp <- Resp[order(Response$yhat,decreasing=TRUE),] ResponseX10 <- do.call(rbind, replicate(10, Resp, simplify=FALSE)) str(ResponseX10) ResponseX10 <- ResponseX10[order(ResponseX10$yhat,decreasing=TRUE),] str(ResponseX10) head(ResponseX10) ResponseX10[[2]] <- NULL ResponseX10 <- data.frame(ResponseX10) str(ResponseX10) cum_R <- cumsum(Response) cum_R sam_size <- nrow(ResponseX10) sam_size cum_n <- seq(1:1,sam_size) cum_n wt <- rep(c(1), times=sam_size) cum_wt <- cumsum(wt) cum_wt dec <- (cum_n/sam_size) decc <- floor((cum_n*10)/(sam_size+1)) str(decc) dec_mean <- aggregate(Response, by=list(decc), mean) dd_ <- data.frame(cum_R, sam_size, cum_wt, cum_n, decc) dd <- cbind(Response, dd_) names(dd)[2] <- "cum_R" dec_mean <- aggregate(Response ~ decc, dd, mean) ****** BR_email wrote: > David: > Hate to bother you, but because you have seen my code perhaps > you can tell me what I am doing wrong. > I want to replicate my original Response data by 0 times, called > ResponseX10. > All is good until the first calculation, cum_R. > Would you kindly, assist me? > Thanks. > Bruce > **** > Response <- data.frame(Response=rbinom(50,1,0.2), yhat=runif(50)) > Response <- Response[order(Response$yhat,decreasing=TRUE),] > > ResponseX10 <- do.call(rbind, replicate(10, Response, simplify=FALSE)) > > ResponseX10 <- ResponseX10[order(ResponseX10$yhat,decreasing=TRUE),] > > ResponseX10[[2]] <- NULL > ResponseX10 > > cum_R <- cumsum(Response) > cum_R > ******* > > Bruce Ratner, Ph.D. > The Significant Statistician™ > (516) 791-3544 > Statistical Predictive Analtyics -- www.DMSTAT1.com > Machine-Learning Data Mining and Modeling -- www.GenIQ.net > > > David L Carlson wrote: >> You can rename the columns with colnames() before passing it to >> xtable. This will let you use characters that data.frame would >> automatically convert. >> >> I don't see an easy way to center the columns when printing an html >> file. If you are not making too many of these, you could open the >> .html file into a WYSIWIG html editor such as BlueGriffon, make the >> changes and save the file. If you have Microsoft Excel and Word, >> another fallback solution is to read the .html file into Excel where >> you have a wide variety of styles. >> >> David C >> >> -----Original Message----- >> From: BR_email [mailto:[hidden email]] >> Sent: Thursday, April 20, 2017 4:31 PM >> To: David L Carlson <[hidden email]>; [hidden email] >> Subject: Re: [R] Looking for a package to replace xtable >> >> David: >> All is perfect, almost - after I ran your corrections. >> Is there a way I can have more control of the column names, i.e., >> not be restricted to abbreviations headings, and center-justify? >> >> Thanks a lot, nice. >> Bruce >> >> >> David L Carlson wrote: >>> #1 You can remove the rownames by adding the argument >>> include.rownames=FALSE to print.xtable(): >>> >>> print.xtable(DECILE_TABLE, type="html",file="DecileTable.html", >>> include.rownames=FALSE) >>> >>> #2 Prevent data.frame from converting the first column to a factor >>> and use NAs for the columns where you don't want totals: >>> >>> dec_mean_wt_R_nRDL <- cbind(DECILE, dec_mean_wt_R_nRD, Cum_Lift, >>> stringsAsFactors=FALSE) >>> dec_mean_wt_R_nRDL <- dec_mean_wt_R_nRDL[,c(1,3,4,9,8,10)] >>> >>> total_line<-cbind(DECILE="Total", >>> as.data.frame(matrix(c(colSums(dec_mean_wt_R_nRDL[ , 2:3]), >>> rep(NA, 3)),nrow=1))) >>> >>> Now the table should print without totals in the last three columns >>> and no rownames. >>> >>> attach is discouraged since it can lead to confusion when a variable >>> name exists in the environment and in a data frame (or multiple data >>> frames). It is easy to forget which version of the variable you are >>> using. More typing, but less subject to confusion would be to use >>> with(), eg: >>> >>> Cum_RespRate <- with(dec_mean_wt_R_nR, (Cum_R/Cum_n)*100) >>> >>> This way it is always clear where Cum_R and Cum_n are coming from. >>> In your code cum_R = Cum_R and cum_n = Cum_n so you could also use >>> >>> Cum_RespRate <- cum_R/cum_n)*100 >>> >>> ------------------------------------- >>> David L Carlson >>> Department of Anthropology >>> Texas A&M University >>> College Station, TX 77840-4352 >>> >>> -----Original Message----- >>> From: R-help [mailto:[hidden email]] On Behalf Of >>> BR_email >>> Sent: Thursday, April 20, 2017 12:10 PM >>> To: [hidden email] >>> Subject: [R] Looking for a package to replace xtable >>> >>> R-helper: >>> Below, code for generating a decile table. >>> I am using the xtable package, but it is not quite right for the >>> output. >>> Issue #1. xtable inserts an unwanted column, before the first derived >>> column DECILE >>> Issue #2. In the last line "Total" I manually sum all columns, even >>> though I only want the sums for second and third columns. >>> If I calculate only second and third columns, the remaining columns >>> would have NAs. >>> Either scenario is not desired. >>> >>> Any suggestions, would be appreciated, for a package that addresses >>> issue #1, >>> and has an option for summing the desired two columns. >>> >>> Lastly, I read that one should rarely use "attach()", but if I don't >>> the >>> program will not run. >>> An explanation of why I need attach() would also be appreciated. >>> Thanks. >>> Bruce >>> >>> **** >>> Response <- data.frame(Response=rbinom(50,1,0.2), yhat=runif(50)) >>> Response <- Response[order(Response$yhat,decreasing=TRUE),] >>> >>> Response[[2]] <- NULL >>> >>> cum_R <- cumsum(Response) >>> sam_size <- nrow(Response) >>> >>> cum_n <- seq(1:1,sam_size) >>> wt <- rep(c(1), times=sam_size) >>> cum_wt <- cumsum(wt) >>> >>> dec <- (cum_n/sam_size) >>> decc <- floor((cum_n*10)/(sam_size+1)) >>> >>> dec_mean <- aggregate(Response, by=list(decc), mean) >>> >>> dd_ <- data.frame(cum_R, sam_size, cum_wt, cum_n, decc) >>> dd <- cbind(Response, dd_) >>> names(dd)[2] <- "cum_R" >>> >>> dec_mean <- aggregate(Response ~ decc, dd, mean) >>> >>> wt <- rep(c(1), times=sam_size) >>> cum_wt <- aggregate(wt ~ decc, dd, sum) >>> cum_R <- aggregate(Response ~ decc, dd, sum) >>> >>> dec_mean_wt <- cbind(dec_mean, cum_wt) >>> dec_mean_wt <- dec_mean_wt[-3] >>> >>> dec_mean_wt_R <- cbind(dec_mean_wt, cum_R) >>> dec_mean_wt_R <- dec_mean_wt_R[-4] >>> >>> colnames(dec_mean_wt_R) <- c("Decile", "Resp_Rate", "No_Inds", >>> "No_Resp") >>> >>> dec_mean_wt_R <- dec_mean_wt_R[,c(1,3,4,2)] >>> >>> cum_n <- dec_mean_wt_R[2] >>> cum_n <- cumsum(cum_n) >>> >>> cum_R <- dec_mean_wt_R[3] >>> cum_R <- cumsum(cum_R) >>> >>> dec_mean_wt_R_nR <- cbind(dec_mean_wt_R, cum_n, cum_R) >>> >>> colnames(dec_mean_wt_R_nR) <- >>> c("Decile", "No_Inds", "No_Resp", "RespRate", >>> "Cum_n", "Cum_R") >>> >>> dec_mean_wt_R_nR >>> >>> attach(dec_mean_wt_R_nR) >>> Cum_RespRate <- (Cum_R/Cum_n)*100 >>> >>> options(digits=4) >>> Decile_RespRate <- (No_Resp/No_Inds) >>> >>> dec_mean_wt_R_nRD <- cbind(dec_mean_wt_R_nR, Cum_RespRate, >>> Decile_RespRate) >>> >>> avg_RR <- dec_mean_wt_R_nRD[10,7] >>> Cum_Lift <- (Cum_RespRate/avg_RR)*100 >>> >>> DECILE <- c("top","2","3","4","5","6","7","8","9","bot") >>> dec_mean_wt_R_nRDL <- cbind(DECILE, dec_mean_wt_R_nRD, Cum_Lift) >>> dec_mean_wt_R_nRDL <- dec_mean_wt_R_nRDL[,c(1,3,4,9,8,10)] >>> >>> total_line<-cbind(DECILE="Total", >>> as.data.frame(matrix(colSums(dec_mean_wt_R_nRDL[-1]),nrow=1))) >>> >>> names(total_line)<-names(dec_mean_wt_R_nRDL) >>> dec_mean_wt_R_nRDLT<-rbind(dec_mean_wt_R_nRDL,total_line) >>> decile_table <- dec_mean_wt_R_nRDLT >>> decile_table >>> >>> #Install the xtable package: install.packages("xtable") >>> #Load the xtable package: >>> library(xtable) >>> >>> DECILE_TABLE <-xtable(decile_table) >>> DECILE_TABLE >>> >>> print.xtable(DECILE_TABLE, >>> type="html",file="C:/R_Data/DecileTable.html") >>> >>> **** >>> >>> -- >>> >>> ______________________________________________ >>> [hidden email] 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. >>> >>> >>> >> >> >> > ______________________________________________ [hidden email] 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. |
You have an issue at the top with
Resp <- data.frame(Response=rbinom(50,1,0.2), yhat=runif(50)) Resp <- Resp[order(Response$yhat,decreasing=TRUE),] Since Response$yhat has not been defined at this point. Presumably you want Resp <- Resp[order(Resp$yhat,decreasing=TRUE),] The main issue is that you have a variable Response that is located in a data frame called ResponseX10. In creating cum_R you need cum_R <- with(ResponseX10, cumsum(Response)) then dec_mean dec_mean <- with(ResponseX10, aggregate(Response, by=list(decc), mean)) then dd dd <- with(ResponseX10, cbind(Response, dd_)) You might consider if Response really needs to be inside a data frame that consists of a single column (maybe you do if you need to keep track of the row numbers). If you just worked with the vector Response, you would not have to use with() or attach(). I'm not sure what the first few lines of your code are intended to do. You choose random binomial values and uniform random values and then order the first by the second. But rbinom() is selecting random values so what is the purpose of randomizing random values? If the real data consist of a vector of 1's and 0's and those need to be randomized, sample(data) will do it for you. Then those numbers are replicated 10 times. Why not just select 500 values using rbinom() initially? David C -----Original Message----- From: BR_email [mailto:[hidden email]] Sent: Friday, April 21, 2017 1:22 PM To: David L Carlson <[hidden email]>; [hidden email] Subject: Re: [R] Looking for a package to replace xtable David: I tried somethings and got a little more working. Now, I am struck at last line provided: "dec_mean <- aggregate(Response ~ decc, dd, mean)" Any help is appreciated. Bruce ***** Resp <- data.frame(Response=rbinom(50,1,0.2), yhat=runif(50)) Resp <- Resp[order(Response$yhat,decreasing=TRUE),] ResponseX10 <- do.call(rbind, replicate(10, Resp, simplify=FALSE)) str(ResponseX10) ResponseX10 <- ResponseX10[order(ResponseX10$yhat,decreasing=TRUE),] str(ResponseX10) head(ResponseX10) ResponseX10[[2]] <- NULL ResponseX10 <- data.frame(ResponseX10) str(ResponseX10) cum_R <- cumsum(Response) cum_R sam_size <- nrow(ResponseX10) sam_size cum_n <- seq(1:1,sam_size) cum_n wt <- rep(c(1), times=sam_size) cum_wt <- cumsum(wt) cum_wt dec <- (cum_n/sam_size) decc <- floor((cum_n*10)/(sam_size+1)) str(decc) dec_mean <- aggregate(Response, by=list(decc), mean) dd_ <- data.frame(cum_R, sam_size, cum_wt, cum_n, decc) dd <- cbind(Response, dd_) names(dd)[2] <- "cum_R" dec_mean <- aggregate(Response ~ decc, dd, mean) ****** BR_email wrote: > David: > Hate to bother you, but because you have seen my code perhaps > you can tell me what I am doing wrong. > I want to replicate my original Response data by 0 times, called > ResponseX10. > All is good until the first calculation, cum_R. > Would you kindly, assist me? > Thanks. > Bruce > **** > Response <- data.frame(Response=rbinom(50,1,0.2), yhat=runif(50)) > Response <- Response[order(Response$yhat,decreasing=TRUE),] > > ResponseX10 <- do.call(rbind, replicate(10, Response, simplify=FALSE)) > > ResponseX10 <- ResponseX10[order(ResponseX10$yhat,decreasing=TRUE),] > > ResponseX10[[2]] <- NULL > ResponseX10 > > cum_R <- cumsum(Response) > cum_R > ******* > > Bruce Ratner, Ph.D. > The Significant Statistician™ > (516) 791-3544 > Statistical Predictive Analtyics -- www.DMSTAT1.com > Machine-Learning Data Mining and Modeling -- www.GenIQ.net > > > David L Carlson wrote: >> You can rename the columns with colnames() before passing it to >> xtable. This will let you use characters that data.frame would >> automatically convert. >> >> I don't see an easy way to center the columns when printing an html >> file. If you are not making too many of these, you could open the >> .html file into a WYSIWIG html editor such as BlueGriffon, make the >> changes and save the file. If you have Microsoft Excel and Word, >> another fallback solution is to read the .html file into Excel where >> you have a wide variety of styles. >> >> David C >> >> -----Original Message----- >> From: BR_email [mailto:[hidden email]] >> Sent: Thursday, April 20, 2017 4:31 PM >> To: David L Carlson <[hidden email]>; [hidden email] >> Subject: Re: [R] Looking for a package to replace xtable >> >> David: >> All is perfect, almost - after I ran your corrections. >> Is there a way I can have more control of the column names, i.e., >> not be restricted to abbreviations headings, and center-justify? >> >> Thanks a lot, nice. >> Bruce >> >> >> David L Carlson wrote: >>> #1 You can remove the rownames by adding the argument >>> include.rownames=FALSE to print.xtable(): >>> >>> print.xtable(DECILE_TABLE, type="html",file="DecileTable.html", >>> include.rownames=FALSE) >>> >>> #2 Prevent data.frame from converting the first column to a factor >>> and use NAs for the columns where you don't want totals: >>> >>> dec_mean_wt_R_nRDL <- cbind(DECILE, dec_mean_wt_R_nRD, Cum_Lift, >>> stringsAsFactors=FALSE) >>> dec_mean_wt_R_nRDL <- dec_mean_wt_R_nRDL[,c(1,3,4,9,8,10)] >>> >>> total_line<-cbind(DECILE="Total", >>> as.data.frame(matrix(c(colSums(dec_mean_wt_R_nRDL[ , 2:3]), >>> rep(NA, 3)),nrow=1))) >>> >>> Now the table should print without totals in the last three columns >>> and no rownames. >>> >>> attach is discouraged since it can lead to confusion when a variable >>> name exists in the environment and in a data frame (or multiple data >>> frames). It is easy to forget which version of the variable you are >>> using. More typing, but less subject to confusion would be to use >>> with(), eg: >>> >>> Cum_RespRate <- with(dec_mean_wt_R_nR, (Cum_R/Cum_n)*100) >>> >>> This way it is always clear where Cum_R and Cum_n are coming from. >>> In your code cum_R = Cum_R and cum_n = Cum_n so you could also use >>> >>> Cum_RespRate <- cum_R/cum_n)*100 >>> >>> ------------------------------------- >>> David L Carlson >>> Department of Anthropology >>> Texas A&M University >>> College Station, TX 77840-4352 >>> >>> -----Original Message----- >>> From: R-help [mailto:[hidden email]] On Behalf Of >>> BR_email >>> Sent: Thursday, April 20, 2017 12:10 PM >>> To: [hidden email] >>> Subject: [R] Looking for a package to replace xtable >>> >>> R-helper: >>> Below, code for generating a decile table. >>> I am using the xtable package, but it is not quite right for the >>> output. >>> Issue #1. xtable inserts an unwanted column, before the first derived >>> column DECILE >>> Issue #2. In the last line "Total" I manually sum all columns, even >>> though I only want the sums for second and third columns. >>> If I calculate only second and third columns, the remaining columns >>> would have NAs. >>> Either scenario is not desired. >>> >>> Any suggestions, would be appreciated, for a package that addresses >>> issue #1, >>> and has an option for summing the desired two columns. >>> >>> Lastly, I read that one should rarely use "attach()", but if I don't >>> the >>> program will not run. >>> An explanation of why I need attach() would also be appreciated. >>> Thanks. >>> Bruce >>> >>> **** >>> Response <- data.frame(Response=rbinom(50,1,0.2), yhat=runif(50)) >>> Response <- Response[order(Response$yhat,decreasing=TRUE),] >>> >>> Response[[2]] <- NULL >>> >>> cum_R <- cumsum(Response) >>> sam_size <- nrow(Response) >>> >>> cum_n <- seq(1:1,sam_size) >>> wt <- rep(c(1), times=sam_size) >>> cum_wt <- cumsum(wt) >>> >>> dec <- (cum_n/sam_size) >>> decc <- floor((cum_n*10)/(sam_size+1)) >>> >>> dec_mean <- aggregate(Response, by=list(decc), mean) >>> >>> dd_ <- data.frame(cum_R, sam_size, cum_wt, cum_n, decc) >>> dd <- cbind(Response, dd_) >>> names(dd)[2] <- "cum_R" >>> >>> dec_mean <- aggregate(Response ~ decc, dd, mean) >>> >>> wt <- rep(c(1), times=sam_size) >>> cum_wt <- aggregate(wt ~ decc, dd, sum) >>> cum_R <- aggregate(Response ~ decc, dd, sum) >>> >>> dec_mean_wt <- cbind(dec_mean, cum_wt) >>> dec_mean_wt <- dec_mean_wt[-3] >>> >>> dec_mean_wt_R <- cbind(dec_mean_wt, cum_R) >>> dec_mean_wt_R <- dec_mean_wt_R[-4] >>> >>> colnames(dec_mean_wt_R) <- c("Decile", "Resp_Rate", "No_Inds", >>> "No_Resp") >>> >>> dec_mean_wt_R <- dec_mean_wt_R[,c(1,3,4,2)] >>> >>> cum_n <- dec_mean_wt_R[2] >>> cum_n <- cumsum(cum_n) >>> >>> cum_R <- dec_mean_wt_R[3] >>> cum_R <- cumsum(cum_R) >>> >>> dec_mean_wt_R_nR <- cbind(dec_mean_wt_R, cum_n, cum_R) >>> >>> colnames(dec_mean_wt_R_nR) <- >>> c("Decile", "No_Inds", "No_Resp", "RespRate", >>> "Cum_n", "Cum_R") >>> >>> dec_mean_wt_R_nR >>> >>> attach(dec_mean_wt_R_nR) >>> Cum_RespRate <- (Cum_R/Cum_n)*100 >>> >>> options(digits=4) >>> Decile_RespRate <- (No_Resp/No_Inds) >>> >>> dec_mean_wt_R_nRD <- cbind(dec_mean_wt_R_nR, Cum_RespRate, >>> Decile_RespRate) >>> >>> avg_RR <- dec_mean_wt_R_nRD[10,7] >>> Cum_Lift <- (Cum_RespRate/avg_RR)*100 >>> >>> DECILE <- c("top","2","3","4","5","6","7","8","9","bot") >>> dec_mean_wt_R_nRDL <- cbind(DECILE, dec_mean_wt_R_nRD, Cum_Lift) >>> dec_mean_wt_R_nRDL <- dec_mean_wt_R_nRDL[,c(1,3,4,9,8,10)] >>> >>> total_line<-cbind(DECILE="Total", >>> as.data.frame(matrix(colSums(dec_mean_wt_R_nRDL[-1]),nrow=1))) >>> >>> names(total_line)<-names(dec_mean_wt_R_nRDL) >>> dec_mean_wt_R_nRDLT<-rbind(dec_mean_wt_R_nRDL,total_line) >>> decile_table <- dec_mean_wt_R_nRDLT >>> decile_table >>> >>> #Install the xtable package: install.packages("xtable") >>> #Load the xtable package: >>> library(xtable) >>> >>> DECILE_TABLE <-xtable(decile_table) >>> DECILE_TABLE >>> >>> print.xtable(DECILE_TABLE, >>> type="html",file="C:/R_Data/DecileTable.html") >>> >>> **** >>> >>> -- >>> >>> ______________________________________________ >>> [hidden email] 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. >>> >>> >>> >> >> >> > ______________________________________________ [hidden email] 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. |
David:
Response=rbinom(50,1,0.2), and yhat=runif(50) are simulating the output of a say logistic model, where Response is actual 0-1 responses, and yhat is the predicted response variable. I usually resample the original data to get some noise out of the data. I find it valuable if I can resample from a large sample than the original. (I know this is viewed by some as unorthodox.) Your point: I only need Response as a column vector. That said, what would you alter, please? Thanks for your time. Regards, Bruce ______________ Bruce Ratner PhD The Significant Statistician™ (516) 791-3544 Statistical Predictive Analytics -- www.DMSTAT1.com Machine-Learning Data Mining -- www.GenIQ.net > On Apr 21, 2017, at 3:43 PM, David L Carlson <[hidden email]> wrote: > > You have an issue at the top with > > Resp <- data.frame(Response=rbinom(50,1,0.2), yhat=runif(50)) > Resp <- Resp[order(Response$yhat,decreasing=TRUE),] > > Since Response$yhat has not been defined at this point. Presumably you want > > Resp <- Resp[order(Resp$yhat,decreasing=TRUE),] > > The main issue is that you have a variable Response that is located in a data frame called ResponseX10. > > In creating cum_R you need > > cum_R <- with(ResponseX10, cumsum(Response)) > > then dec_mean > > dec_mean <- with(ResponseX10, aggregate(Response, by=list(decc), mean)) > > then dd > > dd <- with(ResponseX10, cbind(Response, dd_)) > > > You might consider if Response really needs to be inside a data frame that consists of a single column (maybe you do if you need to keep track of the row numbers). If you just worked with the vector Response, you would not have to use with() or attach(). > > I'm not sure what the first few lines of your code are intended to do. You choose random binomial values and uniform random values and then order the first by the second. But rbinom() is selecting random values so what is the purpose of randomizing random values? If the real data consist of a vector of 1's and 0's and those need to be randomized, sample(data) will do it for you. > > Then those numbers are replicated 10 times. Why not just select 500 values using rbinom() initially? > > > David C > > > -----Original Message----- > From: BR_email [mailto:[hidden email]] > Sent: Friday, April 21, 2017 1:22 PM > To: David L Carlson <[hidden email]>; [hidden email] > Subject: Re: [R] Looking for a package to replace xtable > > David: > I tried somethings and got a little more working. > Now, I am struck at last line provided: "dec_mean <- > aggregate(Response ~ decc, dd, mean)" > Any help is appreciated. > Bruce > > ***** > Resp <- data.frame(Response=rbinom(50,1,0.2), yhat=runif(50)) > Resp <- Resp[order(Response$yhat,decreasing=TRUE),] > > ResponseX10 <- do.call(rbind, replicate(10, Resp, simplify=FALSE)) > str(ResponseX10) > > ResponseX10 <- ResponseX10[order(ResponseX10$yhat,decreasing=TRUE),] > > str(ResponseX10) > head(ResponseX10) > > ResponseX10[[2]] <- NULL > ResponseX10 <- data.frame(ResponseX10) > str(ResponseX10) > > cum_R <- cumsum(Response) > cum_R > > sam_size <- n ______________________________________________ [hidden email] 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. |
David:
Correction: I do need a data frame because from the order Response column I create a data frame as per all my calculated columns, yielding the five column decile table. I used your latest corrections but something buggy is a happenin'. Bruce ______________ Bruce Ratner PhD The Significant Statistician™ (516) 791-3544 Statistical Predictive Analytics -- www.DMSTAT1.com Machine-Learning Data Mining -- www.GenIQ.net > On Apr 21, 2017, at 4:25 PM, Bruce Ratner PhD <[hidden email]> wrote: > > David: > Response=rbinom(50,1,0.2), and yhat=runif(50) are simulating the output of a say logistic model, where Response is actual 0-1 responses, and yhat is the predicted > response variable. > I usually resample the original data to get some noise out of the data. I find it valuable if I can resample from a large sample than the original. > (I know this is viewed by some as unorthodox.) > > Your point: I only need Response as a column vector. > That said, what would you alter, please? > Thanks for your time. > Regards, > Bruce > > ______________ > Bruce Ratner PhD > The Significant Statistician™ > (516) 791-3544 > Statistical Predictive Analytics -- www.DMSTAT1.com > Machine-Learning Data Mining -- www.GenIQ.net > > > >> On Apr 21, 2017, at 3:43 PM, David L Carlson <[hidden email]> wrote: >> >> You have an issue at the top with >> >> Resp <- data.frame(Response=rbinom(50,1,0.2), yhat=runif(50)) >> Resp <- Resp[order(Response$yhat,decreasing=TRUE),] >> >> Since Response$yhat has not been defined at this point. Presumably you want >> >> Resp <- Resp[order(Resp$yhat,decreasing=TRUE),] >> >> The main issue is that you have a variable Response that is located in a data frame called ResponseX10. >> >> In creating cum_R you need >> >> cum_R <- with(ResponseX10, cumsum(Response)) >> >> then dec_mean >> >> dec_mean <- with(ResponseX10, aggregate(Response, by=list(decc), mean)) >> >> then dd >> >> dd <- with(ResponseX10, cbind(Response, dd_)) >> >> >> You might consider if Response really needs to be inside a data frame that consists of a single column (maybe you do if you need to keep track of the row numbers). If you just worked with the vector Response, you would not have to use with() or attach(). >> >> I'm not sure what the first few lines of your code are intended to do. You choose random binomial values and uniform random values and then order the first by the second. But rbinom() is selecting random values so what is the purpose of randomizing random values? If the real data consist of a vector of 1's and 0's and those need to be randomized, sample(data) will do it for you. >> >> Then those numbers are replicated 10 times. Why not just select 500 values using rbinom() initially? >> >> >> David C >> >> >> -----Original Message----- >> From: BR_email [mailto:[hidden email]] >> Sent: Friday, April 21, 2017 1:22 PM >> To: David L Carlson <[hidden email]>; [hidden email] >> Subject: Re: [R] Looking for a package to replace xtable >> >> David: >> I tried somethings and got a little more working. >> Now, I am struck at last line provided: "dec_mean <- >> aggregate(Response ~ decc, dd, mean)" >> Any help is appreciated. >> Bruce >> >> ***** >> Resp <- data.frame(Response=rbinom(50,1,0.2), yhat=runif(50)) >> Resp <- Resp[order(Response$yhat,decreasing=TRUE),] >> >> ResponseX10 <- do.call(rbind, replicate(10, Resp, simplify=FALSE)) >> str(ResponseX10) >> >> ResponseX10 <- ResponseX10[order(ResponseX10$yhat,decreasing=TRUE),] >> >> str(ResponseX10) >> head(ResponseX10) >> >> ResponseX10[[2]] <- NULL >> ResponseX10 <- data.frame(ResponseX10) >> str(ResponseX10) >> >> cum_R <- cumsum(Response) >> cum_R >> >> sam_size <- n ______________________________________________ [hidden email] 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. |
I've attached a modification of your script file (called .txt so it doesn't get stripped). See if this does what you want.
David C -----Original Message----- From: Bruce Ratner PhD [mailto:[hidden email]] Sent: Friday, April 21, 2017 3:46 PM To: David L Carlson <[hidden email]> Cc: [hidden email] Subject: Re: [R] Looking for a package to replace xtable David: Correction: I do need a data frame because from the order Response column I create a data frame as per all my calculated columns, yielding the five column decile table. I used your latest corrections but something buggy is a happenin'. Bruce ______________ Bruce Ratner PhD The Significant Statistician™ (516) 791-3544 Statistical Predictive Analytics -- www.DMSTAT1.com Machine-Learning Data Mining -- www.GenIQ.net > On Apr 21, 2017, at 4:25 PM, Bruce Ratner PhD <[hidden email]> wrote: > > David: > Response=rbinom(50,1,0.2), and yhat=runif(50) are simulating the output of a say logistic model, where Response is actual 0-1 responses, and yhat is the predicted > response variable. > I usually resample the original data to get some noise out of the data. I find it valuable if I can resample from a large sample than the original. > (I know this is viewed by some as unorthodox.) > > Your point: I only need Response as a column vector. > That said, what would you alter, please? > Thanks for your time. > Regards, > Bruce > > ______________ > Bruce Ratner PhD > The Significant Statistician™ > (516) 791-3544 > Statistical Predictive Analytics -- www.DMSTAT1.com > Machine-Learning Data Mining -- www.GenIQ.net > > > >> On Apr 21, 2017, at 3:43 PM, David L Carlson <[hidden email]> wrote: >> >> You have an issue at the top with >> >> Resp <- data.frame(Response=rbinom(50,1,0.2), yhat=runif(50)) >> Resp <- Resp[order(Response$yhat,decreasing=TRUE),] >> >> Since Response$yhat has not been defined at this point. Presumably you want >> >> Resp <- Resp[order(Resp$yhat,decreasing=TRUE),] >> >> The main issue is that you have a variable Response that is located in a data frame called ResponseX10. >> >> In creating cum_R you need >> >> cum_R <- with(ResponseX10, cumsum(Response)) >> >> then dec_mean >> >> dec_mean <- with(ResponseX10, aggregate(Response, by=list(decc), mean)) >> >> then dd >> >> dd <- with(ResponseX10, cbind(Response, dd_)) >> >> >> You might consider if Response really needs to be inside a data frame that consists of a single column (maybe you do if you need to keep track of the row numbers). If you just worked with the vector Response, you would not have to use with() or attach(). >> >> I'm not sure what the first few lines of your code are intended to do. You choose random binomial values and uniform random values and then order the first by the second. But rbinom() is selecting random values so what is the purpose of randomizing random values? If the real data consist of a vector of 1's and 0's and those need to be randomized, sample(data) will do it for you. >> >> Then those numbers are replicated 10 times. Why not just select 500 values using rbinom() initially? >> >> >> David C >> >> >> -----Original Message----- >> From: BR_email [mailto:[hidden email]] >> Sent: Friday, April 21, 2017 1:22 PM >> To: David L Carlson <[hidden email]>; [hidden email] >> Subject: Re: [R] Looking for a package to replace xtable >> >> David: >> I tried somethings and got a little more working. >> Now, I am struck at last line provided: "dec_mean <- >> aggregate(Response ~ decc, dd, mean)" >> Any help is appreciated. >> Bruce >> >> ***** >> Resp <- data.frame(Response=rbinom(50,1,0.2), yhat=runif(50)) >> Resp <- Resp[order(Response$yhat,decreasing=TRUE),] >> >> ResponseX10 <- do.call(rbind, replicate(10, Resp, simplify=FALSE)) >> str(ResponseX10) >> >> ResponseX10 <- ResponseX10[order(ResponseX10$yhat,decreasing=TRUE),] >> >> str(ResponseX10) >> head(ResponseX10) >> >> ResponseX10[[2]] <- NULL >> ResponseX10 <- data.frame(ResponseX10) >> str(ResponseX10) >> >> cum_R <- cumsum(Response) >> cum_R >> >> sam_size <- n ______________________________________________ [hidden email] 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. Bruce.txt (1K) Download Attachment |
David:
Its' not going my way today. I'll try to work with all that you provided. It's late Friday and I cannot impose on you much more. Thank you. If I may, after I rework it, I'll contact you, okay? Have great weekend. And thanks, again. Bruce FYI: Below are where the error messages start: R> Decile_RespRate <- (No_Resp/No_Inds) Error: object 'No_Resp' not found R> R> dec_mean_wt_R_nRD <- cbind(dec_mean_wt_R_nR, Cum_RespRate, Decile_RespRate) Error in cbind(dec_mean_wt_R_nR, Cum_RespRate, Decile_RespRate) : object 'Decile_RespRate' not found R> R> R> R> avg_RR <- dec_mean_wt_R_nRD[10,7] Error: object 'dec_mean_wt_R_nRD' not found R> Cum_Lift <- (Cum_RespRate/avg_RR)*100 Error: object 'avg_RR' not found R> R> DECILE <- c("top","2","3","4","5","6","7","8","9","bot") R> R> dec_mean_wt_R_nRDL <- cbind(DECILE, dec_mean_wt_R_nRD, Cum_Lift, stringsAsFactors=FALSE) Error in cbind(DECILE, dec_mean_wt_R_nRD, Cum_Lift, stringsAsFactors = FALSE) : object 'dec_mean_wt_R_nRD' not found R> dec_mean_wt_R_nRDL <- dec_mean_wt_R_nRDL[,c(1,3,4,9,8,10)] Error: object 'dec_mean_wt_R_nRDL' not found R> R> R> total_line<-cbind(DECILE="Total", + as.data.frame(matrix(c(colSums(dec_mean_wt_R_nRDL[ , 2:3]), rep(NA, 3)),nrow=1))) Error in is.data.frame(x) : object 'dec_mean_wt_R_nRDL' not found R> R> names(total_line)<-names(dec_mean_wt_R_nRDL) Error: object 'dec_mean_wt_R_nRDL' not found R> dec_mean_wt_R_nRDLT<-rbind(dec_mean_wt_R_nRDL,total_line) Error in rbind(dec_mean_wt_R_nRDL, total_line) : object 'dec_mean_wt_R_nRDL' not found R> decile_table <- dec_mean_wt_R_nRDLT Error: object 'dec_mean_wt_R_nRDLT' not found R> decile_table Error: object 'decile_table' not found R> R> #Install the xtable package: install.packages("xtable") R> #Load the xtable package: R> library(xtable) R> R> DECILE_TABLE <-xtable(decile_table) Error in xtable(decile_table) : object 'decile_table' not found R> DECILE_TABLE Error: object 'DECILE_TABLE' not found R> R> R> print.xtable(DECILE_TABLE, type="html",file="C:/R_Data/DecileTable.html", include.rownames=FALSE) Error in print.xtable(DECILE_TABLE, type = "html", file = "C:/R_Data/DecileTable.html", : object 'DECILE_TABLE' not found R> Bruce Ratner, Ph.D. The Significant Statistician™ (516) 791-3544 Statistical Predictive Analtyics -- www.DMSTAT1.com Machine-Learning Data Mining and Modeling -- www.GenIQ.net David L Carlson wrote: > I've attached a modification of your script file (called .txt so it doesn't get stripped). See if this does what you want. > > David C > > -----Original Message----- > From: Bruce Ratner PhD [mailto:[hidden email]] > Sent: Friday, April 21, 2017 3:46 PM > To: David L Carlson <[hidden email]> > Cc: [hidden email] > Subject: Re: [R] Looking for a package to replace xtable > > David: > Correction: I do need a data frame because from the order Response column I create a data frame as per all my calculated columns, yielding the five column decile table. > I used your latest corrections but something buggy is a happenin'. > > Bruce > ______________ > Bruce Ratner PhD > The Significant Statistician™ > (516) 791-3544 > Statistical Predictive Analytics -- www.DMSTAT1.com > Machine-Learning Data Mining -- www.GenIQ.net > > > >> On Apr 21, 2017, at 4:25 PM, Bruce Ratner PhD <[hidden email]> wrote: >> >> David: >> Response=rbinom(50,1,0.2), and yhat=runif(50) are simulating the output of a say logistic model, where Response is actual 0-1 responses, and yhat is the predicted >> response variable. >> I usually resample the original data to get some noise out of the data. I find it valuable if I can resample from a large sample than the original. >> (I know this is viewed by some as unorthodox.) >> >> Your point: I only need Response as a column vector. >> That said, what would you alter, please? >> Thanks for your time. >> Regards, >> Bruce >> >> ______________ >> Bruce Ratner PhD >> The Significant Statistician™ >> (516) 791-3544 >> Statistical Predictive Analytics -- www.DMSTAT1.com >> Machine-Learning Data Mining -- www.GenIQ.net >> >> >> >>> On Apr 21, 2017, at 3:43 PM, David L Carlson <[hidden email]> wrote: >>> >>> You have an issue at the top with >>> >>> Resp <- data.frame(Response=rbinom(50,1,0.2), yhat=runif(50)) >>> Resp <- Resp[order(Response$yhat,decreasing=TRUE),] >>> >>> Since Response$yhat has not been defined at this point. Presumably you want >>> >>> Resp <- Resp[order(Resp$yhat,decreasing=TRUE),] >>> >>> The main issue is that you have a variable Response that is located in a data frame called ResponseX10. >>> >>> In creating cum_R you need >>> >>> cum_R <- with(ResponseX10, cumsum(Response)) >>> >>> then dec_mean >>> >>> dec_mean <- with(ResponseX10, aggregate(Response, by=list(decc), mean)) >>> >>> then dd >>> >>> dd <- with(ResponseX10, cbind(Response, dd_)) >>> >>> >>> You might consider if Response really needs to be inside a data frame that consists of a single column (maybe you do if you need to keep track of the row numbers). If you just worked with the vector Response, you would not have to use with() or attach(). >>> >>> I'm not sure what the first few lines of your code are intended to do. You choose random binomial values and uniform random values and then order the first by the second. But rbinom() is selecting random values so what is the purpose of randomizing random values? If the real data consist of a vector of 1's and 0's and those need to be randomized, sample(data) will do it for you. >>> >>> Then those numbers are replicated 10 times. Why not just select 500 values using rbinom() initially? >>> >>> >>> David C >>> >>> >>> -----Original Message----- >>> From: BR_email [mailto:[hidden email]] >>> Sent: Friday, April 21, 2017 1:22 PM >>> To: David L Carlson <[hidden email]>; [hidden email] >>> Subject: Re: [R] Looking for a package to replace xtable >>> >>> David: >>> I tried somethings and got a little more working. >>> Now, I am struck at last line provided: "dec_mean <- >>> aggregate(Response ~ decc, dd, mean)" >>> Any help is appreciated. >>> Bruce >>> >>> ***** >>> Resp <- data.frame(Response=rbinom(50,1,0.2), yhat=runif(50)) >>> Resp <- Resp[order(Response$yhat,decreasing=TRUE),] >>> >>> ResponseX10 <- do.call(rbind, replicate(10, Resp, simplify=FALSE)) >>> str(ResponseX10) >>> >>> ResponseX10 <- ResponseX10[order(ResponseX10$yhat,decreasing=TRUE),] >>> >>> str(ResponseX10) >>> head(ResponseX10) >>> >>> ResponseX10[[2]] <- NULL >>> ResponseX10 <- data.frame(ResponseX10) >>> str(ResponseX10) >>> >>> cum_R <- cumsum(Response) >>> cum_R >>> >>> sam_size <- n ______________________________________________ [hidden email] 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. |
David:
This is my last query on this issue. I thank you, again, for your patience, without perhaps your not understanding what drives (bugs) me. Is there a more efficient way of writing the code, below, which is replicating the original dataset Response 5 times? Response5x <- rbind(Response, Response, Response, Response, Response) Regards, Bruce BR_email wrote: > David: > Its' not going my way today. > I'll try to work with all that you provided. > It's late Friday and I cannot impose on you much more. > Thank you. > If I may, after I rework it, I'll contact you, okay? > Have great weekend. And thanks, again. > Bruce > FYI: Below are where the error messages start: > > R> Decile_RespRate <- (No_Resp/No_Inds) Error: object 'No_Resp' not > found R> R> dec_mean_wt_R_nRD <- cbind(dec_mean_wt_R_nR, Cum_RespRate, > Decile_RespRate) Error in cbind(dec_mean_wt_R_nR, Cum_RespRate, > Decile_RespRate) : object 'Decile_RespRate' not found R> R> R> R> > avg_RR <- dec_mean_wt_R_nRD[10,7] Error: object 'dec_mean_wt_R_nRD' > not found R> Cum_Lift <- (Cum_RespRate/avg_RR)*100 Error: object > 'avg_RR' not found R> R> DECILE <- > c("top","2","3","4","5","6","7","8","9","bot") R> R> > dec_mean_wt_R_nRDL <- cbind(DECILE, dec_mean_wt_R_nRD, Cum_Lift, > stringsAsFactors=FALSE) Error in cbind(DECILE, dec_mean_wt_R_nRD, > Cum_Lift, stringsAsFactors = FALSE) : object 'dec_mean_wt_R_nRD' not > found R> dec_mean_wt_R_nRDL <- dec_mean_wt_R_nRDL[,c(1,3,4,9,8,10)] > Error: object 'dec_mean_wt_R_nRDL' not found R> R> R> > total_line<-cbind(DECILE="Total", + > as.data.frame(matrix(c(colSums(dec_mean_wt_R_nRDL[ , 2:3]), rep(NA, > 3)),nrow=1))) Error in is.data.frame(x) : object 'dec_mean_wt_R_nRDL' > not found R> R> names(total_line)<-names(dec_mean_wt_R_nRDL) Error: > object 'dec_mean_wt_R_nRDL' not found R> > dec_mean_wt_R_nRDLT<-rbind(dec_mean_wt_R_nRDL,total_line) Error in > rbind(dec_mean_wt_R_nRDL, total_line) : object 'dec_mean_wt_R_nRDL' > not found R> decile_table <- dec_mean_wt_R_nRDLT Error: object > 'dec_mean_wt_R_nRDLT' not found R> decile_table Error: object > 'decile_table' not found R> R> #Install the xtable package: > install.packages("xtable") R> #Load the xtable package: R> > library(xtable) R> R> DECILE_TABLE <-xtable(decile_table) Error in > xtable(decile_table) : object 'decile_table' not found R> DECILE_TABLE > Error: object 'DECILE_TABLE' not found R> R> R> > print.xtable(DECILE_TABLE, > type="html",file="C:/R_Data/DecileTable.html", include.rownames=FALSE) > Error in print.xtable(DECILE_TABLE, type = "html", file = > "C:/R_Data/DecileTable.html", : object 'DECILE_TABLE' not found > > R> > > > Bruce Ratner, Ph.D. > The Significant Statistician™ > (516) 791-3544 > Statistical Predictive Analtyics -- www.DMSTAT1.com > Machine-Learning Data Mining and Modeling -- www.GenIQ.net > > > David L Carlson wrote: >> I've attached a modification of your script file (called .txt so it >> doesn't get stripped). See if this does what you want. >> >> David C >> >> -----Original Message----- >> From: Bruce Ratner PhD [mailto:[hidden email]] >> Sent: Friday, April 21, 2017 3:46 PM >> To: David L Carlson <[hidden email]> >> Cc: [hidden email] >> Subject: Re: [R] Looking for a package to replace xtable >> >> David: >> Correction: I do need a data frame because from the order Response >> column I create a data frame as per all my calculated columns, >> yielding the five column decile table. >> I used your latest corrections but something buggy is a happenin'. >> >> Bruce >> ______________ >> Bruce Ratner PhD >> The Significant Statistician™ >> (516) 791-3544 >> Statistical Predictive Analytics -- www.DMSTAT1.com >> Machine-Learning Data Mining -- www.GenIQ.net >> >> >> >>> On Apr 21, 2017, at 4:25 PM, Bruce Ratner PhD <[hidden email]> wrote: >>> >>> David: >>> Response=rbinom(50,1,0.2), and yhat=runif(50) are simulating the >>> output of a say logistic model, where Response is actual 0-1 >>> responses, and yhat is the predicted >>> response variable. >>> I usually resample the original data to get some noise out of the >>> data. I find it valuable if I can resample from a large sample than >>> the original. >>> (I know this is viewed by some as unorthodox.) >>> >>> Your point: I only need Response as a column vector. >>> That said, what would you alter, please? >>> Thanks for your time. >>> Regards, >>> Bruce >>> >>> ______________ >>> Bruce Ratner PhD >>> The Significant Statistician™ >>> (516) 791-3544 >>> Statistical Predictive Analytics -- www.DMSTAT1.com >>> Machine-Learning Data Mining -- www.GenIQ.net >>> >>> >>> >>>> On Apr 21, 2017, at 3:43 PM, David L Carlson <[hidden email]> >>>> wrote: >>>> >>>> You have an issue at the top with >>>> >>>> Resp <- data.frame(Response=rbinom(50,1,0.2), yhat=runif(50)) >>>> Resp <- Resp[order(Response$yhat,decreasing=TRUE),] >>>> >>>> Since Response$yhat has not been defined at this point. Presumably >>>> you want >>>> >>>> Resp <- Resp[order(Resp$yhat,decreasing=TRUE),] >>>> >>>> The main issue is that you have a variable Response that is located >>>> in a data frame called ResponseX10. >>>> >>>> In creating cum_R you need >>>> >>>> cum_R <- with(ResponseX10, cumsum(Response)) >>>> >>>> then dec_mean >>>> >>>> dec_mean <- with(ResponseX10, aggregate(Response, by=list(decc), >>>> mean)) >>>> >>>> then dd >>>> >>>> dd <- with(ResponseX10, cbind(Response, dd_)) >>>> >>>> >>>> You might consider if Response really needs to be inside a data >>>> frame that consists of a single column (maybe you do if you need to >>>> keep track of the row numbers). If you just worked with the vector >>>> Response, you would not have to use with() or attach(). >>>> >>>> I'm not sure what the first few lines of your code are intended to >>>> do. You choose random binomial values and uniform random values and >>>> then order the first by the second. But rbinom() is selecting >>>> random values so what is the purpose of randomizing random values? >>>> If the real data consist of a vector of 1's and 0's and those need >>>> to be randomized, sample(data) will do it for you. >>>> >>>> Then those numbers are replicated 10 times. Why not just select 500 >>>> values using rbinom() initially? >>>> >>>> >>>> David C >>>> >>>> >>>> -----Original Message----- >>>> From: BR_email [mailto:[hidden email]] >>>> Sent: Friday, April 21, 2017 1:22 PM >>>> To: David L Carlson <[hidden email]>; [hidden email] >>>> Subject: Re: [R] Looking for a package to replace xtable >>>> >>>> David: >>>> I tried somethings and got a little more working. >>>> Now, I am struck at last line provided: "dec_mean <- >>>> aggregate(Response ~ decc, dd, mean)" >>>> Any help is appreciated. >>>> Bruce >>>> >>>> ***** >>>> Resp <- data.frame(Response=rbinom(50,1,0.2), yhat=runif(50)) >>>> Resp <- Resp[order(Response$yhat,decreasing=TRUE),] >>>> >>>> ResponseX10 <- do.call(rbind, replicate(10, Resp, simplify=FALSE)) >>>> str(ResponseX10) >>>> >>>> ResponseX10 <- >>>> ResponseX10[order(ResponseX10$yhat,decreasing=TRUE),] >>>> >>>> str(ResponseX10) >>>> head(ResponseX10) >>>> >>>> ResponseX10[[2]] <- NULL >>>> ResponseX10 <- data.frame(ResponseX10) >>>> str(ResponseX10) >>>> >>>> cum_R <- cumsum(Response) >>>> cum_R >>>> >>>> sam_size <- n > ______________________________________________ [hidden email] 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. |
Have you looked at library(sos)? It includes the following that might
help you find what you want: * "findFn" function that searches help pages for matches to your search term, then sorts the results by package. The print method displays the results in a web browser. * "packageSum" to summarize the results by package. * "writeFinFn2xls" to write an Excel file with sheets for package summary and individual help pages found. * "installPackages" to automatically install the packages found most relevant, so "packageSum" and "writeFindFn2xls" can provide more information. * a vignette, also available at "https://journal.r-project.org/archive/2009/RJ-2009-017/RJ-2009-017.pdf". hope this helps Spencer Graves On 2017-04-22 6:47 AM, BR_email wrote: > David: > This is my last query on this issue. > I thank you, again, for your patience, > without perhaps your not understanding what drives (bugs) me. > > Is there a more efficient way of writing the code, below, > which is replicating the original dataset Response 5 times? > > Response5x <- rbind(Response, Response, Response, Response, Response) > > Regards, > Bruce > > > > BR_email wrote: >> David: >> Its' not going my way today. >> I'll try to work with all that you provided. >> It's late Friday and I cannot impose on you much more. >> Thank you. >> If I may, after I rework it, I'll contact you, okay? >> Have great weekend. And thanks, again. >> Bruce >> FYI: Below are where the error messages start: >> >> R> Decile_RespRate <- (No_Resp/No_Inds) Error: object 'No_Resp' not >> found R> R> dec_mean_wt_R_nRD <- cbind(dec_mean_wt_R_nR, >> Cum_RespRate, Decile_RespRate) Error in cbind(dec_mean_wt_R_nR, >> Cum_RespRate, Decile_RespRate) : object 'Decile_RespRate' not found >> R> R> R> R> avg_RR <- dec_mean_wt_R_nRD[10,7] Error: object >> 'dec_mean_wt_R_nRD' not found R> Cum_Lift <- >> (Cum_RespRate/avg_RR)*100 Error: object 'avg_RR' not found R> R> >> DECILE <- c("top","2","3","4","5","6","7","8","9","bot") R> R> >> dec_mean_wt_R_nRDL <- cbind(DECILE, dec_mean_wt_R_nRD, Cum_Lift, >> stringsAsFactors=FALSE) Error in cbind(DECILE, dec_mean_wt_R_nRD, >> Cum_Lift, stringsAsFactors = FALSE) : object 'dec_mean_wt_R_nRD' not >> found R> dec_mean_wt_R_nRDL <- dec_mean_wt_R_nRDL[,c(1,3,4,9,8,10)] >> Error: object 'dec_mean_wt_R_nRDL' not found R> R> R> >> total_line<-cbind(DECILE="Total", + >> as.data.frame(matrix(c(colSums(dec_mean_wt_R_nRDL[ , 2:3]), rep(NA, >> 3)),nrow=1))) Error in is.data.frame(x) : object 'dec_mean_wt_R_nRDL' >> not found R> R> names(total_line)<-names(dec_mean_wt_R_nRDL) Error: >> object 'dec_mean_wt_R_nRDL' not found R> >> dec_mean_wt_R_nRDLT<-rbind(dec_mean_wt_R_nRDL,total_line) Error in >> rbind(dec_mean_wt_R_nRDL, total_line) : object 'dec_mean_wt_R_nRDL' >> not found R> decile_table <- dec_mean_wt_R_nRDLT Error: object >> 'dec_mean_wt_R_nRDLT' not found R> decile_table Error: object >> 'decile_table' not found R> R> #Install the xtable package: >> install.packages("xtable") R> #Load the xtable package: R> >> library(xtable) R> R> DECILE_TABLE <-xtable(decile_table) Error in >> xtable(decile_table) : object 'decile_table' not found R> >> DECILE_TABLE Error: object 'DECILE_TABLE' not found R> R> R> >> print.xtable(DECILE_TABLE, >> type="html",file="C:/R_Data/DecileTable.html", >> include.rownames=FALSE) Error in print.xtable(DECILE_TABLE, type = >> "html", file = "C:/R_Data/DecileTable.html", : object 'DECILE_TABLE' >> not found >> >> R> >> >> >> Bruce Ratner, Ph.D. >> The Significant Statistician™ >> (516) 791-3544 >> Statistical Predictive Analtyics -- www.DMSTAT1.com >> Machine-Learning Data Mining and Modeling -- www.GenIQ.net >> >> >> David L Carlson wrote: >>> I've attached a modification of your script file (called .txt so it >>> doesn't get stripped). See if this does what you want. >>> >>> David C >>> >>> -----Original Message----- >>> From: Bruce Ratner PhD [mailto:[hidden email]] >>> Sent: Friday, April 21, 2017 3:46 PM >>> To: David L Carlson <[hidden email]> >>> Cc: [hidden email] >>> Subject: Re: [R] Looking for a package to replace xtable >>> >>> David: >>> Correction: I do need a data frame because from the order Response >>> column I create a data frame as per all my calculated columns, >>> yielding the five column decile table. >>> I used your latest corrections but something buggy is a happenin'. >>> >>> Bruce >>> ______________ >>> Bruce Ratner PhD >>> The Significant Statistician™ >>> (516) 791-3544 >>> Statistical Predictive Analytics -- www.DMSTAT1.com >>> Machine-Learning Data Mining -- www.GenIQ.net >>> >>> >>> >>>> On Apr 21, 2017, at 4:25 PM, Bruce Ratner PhD <[hidden email]> wrote: >>>> >>>> David: >>>> Response=rbinom(50,1,0.2), and yhat=runif(50) are simulating the >>>> output of a say logistic model, where Response is actual 0-1 >>>> responses, and yhat is the predicted >>>> response variable. >>>> I usually resample the original data to get some noise out of the >>>> data. I find it valuable if I can resample from a large sample than >>>> the original. >>>> (I know this is viewed by some as unorthodox.) >>>> >>>> Your point: I only need Response as a column vector. >>>> That said, what would you alter, please? >>>> Thanks for your time. >>>> Regards, >>>> Bruce >>>> >>>> ______________ >>>> Bruce Ratner PhD >>>> The Significant Statistician™ >>>> (516) 791-3544 >>>> Statistical Predictive Analytics -- www.DMSTAT1.com >>>> Machine-Learning Data Mining -- www.GenIQ.net >>>> >>>> >>>> >>>>> On Apr 21, 2017, at 3:43 PM, David L Carlson <[hidden email]> >>>>> wrote: >>>>> >>>>> You have an issue at the top with >>>>> >>>>> Resp <- data.frame(Response=rbinom(50,1,0.2), yhat=runif(50)) >>>>> Resp <- Resp[order(Response$yhat,decreasing=TRUE),] >>>>> >>>>> Since Response$yhat has not been defined at this point. Presumably >>>>> you want >>>>> >>>>> Resp <- Resp[order(Resp$yhat,decreasing=TRUE),] >>>>> >>>>> The main issue is that you have a variable Response that is >>>>> located in a data frame called ResponseX10. >>>>> >>>>> In creating cum_R you need >>>>> >>>>> cum_R <- with(ResponseX10, cumsum(Response)) >>>>> >>>>> then dec_mean >>>>> >>>>> dec_mean <- with(ResponseX10, aggregate(Response, by=list(decc), >>>>> mean)) >>>>> >>>>> then dd >>>>> >>>>> dd <- with(ResponseX10, cbind(Response, dd_)) >>>>> >>>>> >>>>> You might consider if Response really needs to be inside a data >>>>> frame that consists of a single column (maybe you do if you need >>>>> to keep track of the row numbers). If you just worked with the >>>>> vector Response, you would not have to use with() or attach(). >>>>> >>>>> I'm not sure what the first few lines of your code are intended to >>>>> do. You choose random binomial values and uniform random values >>>>> and then order the first by the second. But rbinom() is selecting >>>>> random values so what is the purpose of randomizing random values? >>>>> If the real data consist of a vector of 1's and 0's and those need >>>>> to be randomized, sample(data) will do it for you. >>>>> >>>>> Then those numbers are replicated 10 times. Why not just select >>>>> 500 values using rbinom() initially? >>>>> >>>>> >>>>> David C >>>>> >>>>> >>>>> -----Original Message----- >>>>> From: BR_email [mailto:[hidden email]] >>>>> Sent: Friday, April 21, 2017 1:22 PM >>>>> To: David L Carlson <[hidden email]>; [hidden email] >>>>> Subject: Re: [R] Looking for a package to replace xtable >>>>> >>>>> David: >>>>> I tried somethings and got a little more working. >>>>> Now, I am struck at last line provided: "dec_mean <- >>>>> aggregate(Response ~ decc, dd, mean)" >>>>> Any help is appreciated. >>>>> Bruce >>>>> >>>>> ***** >>>>> Resp <- data.frame(Response=rbinom(50,1,0.2), yhat=runif(50)) >>>>> Resp <- Resp[order(Response$yhat,decreasing=TRUE),] >>>>> >>>>> ResponseX10 <- do.call(rbind, replicate(10, Resp, simplify=FALSE)) >>>>> str(ResponseX10) >>>>> >>>>> ResponseX10 <- >>>>> ResponseX10[order(ResponseX10$yhat,decreasing=TRUE),] >>>>> >>>>> str(ResponseX10) >>>>> head(ResponseX10) >>>>> >>>>> ResponseX10[[2]] <- NULL >>>>> ResponseX10 <- data.frame(ResponseX10) >>>>> str(ResponseX10) >>>>> >>>>> cum_R <- cumsum(Response) >>>>> cum_R >>>>> >>>>> sam_size <- n >> > > ______________________________________________ > [hidden email] 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. ______________________________________________ [hidden email] 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. |
Free forum by Nabble | Edit this page |