[R] Injecting a column of characters to a matrix of numerics

Ben Tupper ben.bighair at gmail.com
Wed Oct 29 16:52:48 CET 2014


Hi,

On Oct 29, 2014, at 11:41 AM, Steven Yen <syen04 at gmail.com> wrote:

> Hello
> I am designing a regression printout, which works out nicely. Then, I try to inject a column of characters to indicate a discrete regressor with a dot (.). Then, all numbers seem to turn into characters, in quotations. Is there a way to do this right? Below, I show the lines of codes before and after. Thanks.
> Steven Yen
> 
> ---
> out<-round(cbind(me,se,t,p),digits)
> colnames(out)<-c("estimates","s.e.","|t-value|","p-value")
> rownames(out)<-rownames(me)
> out
>            estimates     s.e. |t-value|  p-value
> (Intercept)  0.223263 0.146167  1.527459 0.127173
> sex          0.049830 0.039612  1.257973 0.208890
> age         -0.070423 0.029539  2.384035 0.017433
> yrmarry      0.015567 0.005298  2.938126 0.003429
> children     0.060525 0.044778  1.351659 0.176993
> religius    -0.053128 0.014413  3.686260 0.000248
> educ         0.003226 0.008453  0.381636 0.702866
> occu         0.003915 0.011860  0.330147 0.741404
> rating      -0.077856 0.014466  5.381925 0.000000
> 
> out<-round(cbind(me,se,t,p),digits); out<-cbind(out,disc)
> colnames(out)<-c("estimates","s.e.","|t-value|","p-value","disc")
> rownames(out)<-rownames(me)
> 
> (Intercept) "0.223263"  "0.146167" "1.527459" "0.127173" ""
> sex         "0.04983"   "0.039612" "1.257973" "0.20889"  "."
> age         "-0.070423" "0.029539" "2.384035" "0.017433" ""
> yrmarry     "0.015567"  "0.005298" "2.938126" "0.003429" ""
> children    "0.060525"  "0.044778" "1.351659" "0.176993" "."
> religius    "-0.053128" "0.014413" "3.68626"  "0.000248" ""
> educ        "0.003226"  "0.008453" "0.381636" "0.702866" ""
> occu        "0.003915"  "0.01186"  "0.330147" "0.741404" ""
> rating      "-0.077856" "0.014466" "5.381925" "0"        ""
> 


It appears the 'out' is originally a numeric matrix, thus adding column of characters demotes the entire matrix to character.  Convert 'out' to data.frame to allow mixed data types.

> out<-round(cbind(me,se,t,p),digits); out <- as.data.frame(out) ; out<-cbind(out,disc)


Cheers,
Ben

> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list