[R] cbind() function : Not able to display columns

Jeff Newmiller jdnewmil at dcn.davis.CA.us
Wed Oct 23 06:01:50 CEST 2013


Hard to say, not sure what you want to do. But the columns are not denoted by [a], [b] or [c]. You should learn to use the str function to understand what various expressions really are, and return to the "Introduction to R" document that comes with the software. There is a distinct difference between a and "a" in R, and square brackets are not at all like quotes. See help("[") and the ItoR section on indexing.

You might get what you want by k[,"b"] for example.
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
--------------------------------------------------------------------------- 
Sent from my phone. Please excuse my brevity.

Vivek Singh <vksingh.iiitb at gmail.com> wrote:
>Hi All,
>
>
>I have create a matrix using cbind() function as follows:
>
>
>> a=c(1,2,3)
>
>> b=c('a','b','c')
>
>> c=c("ee","tt","rr")
>
>
>> k=cbind(a,b,c)
>
>
>Problem: when we print the matrix k,
>
>> k
>
>    a   b   c
>
>[1,] "1" "a" "ee"
>
>[2,] "2" "b" "tt"
>
>[3,] "3" "c" "rr"
>
>we can see that rows are represented by [1,] , [2,] and [3,].
>Similarly,
>the columns are denoted by [a], [b] and [c]. When we try to print the
>corresponding columns, we are able to print for k[a], i.e., the first
>column but not able to correctly print the second and third columns.
>
>> k[a]
>
>[1] "1" "2" "3"
>
>> k[b]
>
>[1] NA NA NA
>
>> k[c]
>
>[1] NA NA NA
>
>Please let me know what am I doing wrong.



More information about the R-help mailing list