[R] cbind() function : Not able to display columns
Richard M. Heiberger
rmh at temple.edu
Wed Oct 23 06:23:54 CEST 2013
In addition to what the others have told you, it looks like you might
be confusing
matrices with data.frames. Please see
?data.frame
I think what you are looking for is
> b <- c('a','b','c')
> c <- c("ee","tt","rr")
> k <- cbind(a,b,c)
> K <- data.frame(a, b, c)
> K
a b c
1 1 a ee
2 2 b tt
3 3 c rr
I recommend using " <- " for assignment (not the spaces on both sides), not "=".
On Wed, Oct 23, 2013 at 12:01 AM, Jeff Newmiller
<jdnewmil at dcn.davis.ca.us> wrote:
> 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.
>
> ______________________________________________
> 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