[R] Cbind query

jim holtman jholtman at gmail.com
Fri Oct 22 14:12:10 CEST 2010


This comes about since when using read.table (which I assume you did,
but you did not show us what commands you were using), characters are
converted to factors.  If you are not using factors, then you probably
want the data read in as characters.  You should understand the use of
'str' to look at the structure of your objects to understand what
might be happening.  See example below:

> tab <- read.table(textConnection("  one         two
+ 1  apple      fruit
+ 2  ball         game
+ 3  chair       wood
+ 4  wood       plain
+ 5  fruit        banana
+ 6  cloth       silk"))
> closeAllConnections()
> str(tab)  # note the you have 'factors'
'data.frame':   6 obs. of  2 variables:
 $ one: Factor w/ 6 levels "apple","ball",..: 1 2 3 6 5 4
 $ two: Factor w/ 6 levels "banana","fruit",..: 2 3 6 4 1 5
> cbind(tab$one, tab$two)  # this gives numeric values of the factors
     [,1] [,2]
[1,]    1    2
[2,]    2    3
[3,]    3    6
[4,]    6    4
[5,]    5    1
[6,]    4    5
> # now read in data and not convert to factors (note:  as.is=TRUE)
> tab <- read.table(textConnection("  one         two
+ 1  apple      fruit
+ 2  ball         game
+ 3  chair       wood
+ 4  wood       plain
+ 5  fruit        banana
+ 6  cloth       silk"), as.is = TRUE)
> closeAllConnections()
> str(tab)  # now you have characters
'data.frame':   6 obs. of  2 variables:
 $ one: chr  "apple" "ball" "chair" "wood" ...
 $ two: chr  "fruit" "game" "wood" "plain" ...
> cbind(tab$one, tab$two)  # this gives character values
     [,1]    [,2]
[1,] "apple" "fruit"
[2,] "ball"  "game"
[3,] "chair" "wood"
[4,] "wood"  "plain"
[5,] "fruit" "banana"
[6,] "cloth" "silk"
>


On Fri, Oct 22, 2010 at 7:06 AM, karthicklakshman
<karthick.lakshman at gmail.com> wrote:
>
> I am new to R and request your kind help.
>
> I have a table like the one below,
>   one         two
> 1  apple      fruit
> 2  ball         game
> 3  chair       wood
> 4  wood       plain
> 5  fruit        banana
> 6  cloth       silk
>
> Note: duplicate entries are there
>
> the task is to create relations to each each row entries, like "apple ->
> fruit" . when I tried to combine column1 with column 2 (one, two), using
> "cbind" the string is changed to numerical value...something like this
>        [,1] [,2]
>   [1,]   10   53
>   [2,]   25  562
>   [3,]   25  462
>   [4,]   25 1045
>   [5,]   25  488
>   [6,]   26 1062
>   [7,]   27  951
>   [8,]   27  144
>   [9,]   27  676
>  [10,]   27  486
>
> Please suggest me how to get the string names back like the first table in
> the out put, using cbind.
>
> Thanks in advance
> regards
> kaarz
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Cbind-query-tp3006988p3006988.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?



More information about the R-help mailing list