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,> ka 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. -- Thanks and Regards, Vivek Kumar Singh Research Assistant, School of Computing, National University of Singapore Mobile:(0065) 82721535 [[alternative HTML version deleted]]
Hi, Try: ? k[,"a"] #[1] "1" "2" "3" ?k[,"b"] #[1] "a" "b" "c" ?k[,"c"] #[1] "ee" "tt" "rr" A.K. On Tuesday, October 22, 2013 11:37 PM, 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. -- Thanks and Regards, Vivek Kumar Singh Research Assistant, School of Computing, National University of Singapore Mobile:(0065) 82721535 ??? [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
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.