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.
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?
Hi I am a bit puzzled what you want to do? r-help-bounces at r-project.org napsal dne 22.10.2010 13:06:17:> > 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 486l1<-sample(letters[1:5], 10, rep=T) l2<-sample(LETTERS[1:5], 10, rep=T) cbind(l1, l2) l1 l2 [1,] "a" "A" [2,] "a" "A" [9,] "c" "C" [10,] "b" "B" dat<-data.frame(l1, l2) changes character values to factor dat l1 l2 1 a A 2 a A 9 c C 10 b B str(dat) 'data.frame': 10 obs. of 2 variables: $ l1: Factor w/ 4 levels "a","b","c","d": 1 1 4 1 4 1 4 1 3 2 $ l2: Factor w/ 5 levels "A","B","C","D",..: 1 1 4 4 4 2 4 5 3 2 and cbind turns them to numeric cbind(dat$l1, dat$l2) [,1] [,2] [1,] 1 1 [2,] 1 1 [9,] 3 3 [10,] 2 2 So you shall turn your factors to character values. But without more info from your side it is just a speculation. Regards Petr> > Please suggest me how to get the string names back like the first tablein> 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 guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
Hello jholtman, Thanks very much for the suggestion. I tried using "as.is=TRUE" and it worked as the way I expected. Sorry for not being clear about the problem in my mail. The characters are very much needed, cos I am trying to create a signaling network using "Rgraphviz". Thanks again. Regards, kaarz -- View this message in context: http://r.789695.n4.nabble.com/Cbind-query-tp3006988p3008190.html Sent from the R help mailing list archive at Nabble.com.
Hello Petr Pikal, Thanks for the suggestion. I am sorry for not being very clear in my mail. from your example, Its clear to me now about how to turn factors to character values. I am working on large data sets, and the ultimate aim is to create interaction network, so the characters are very important and your post is of true value. Thanks for your inputs Regards, karthick -- View this message in context: http://r.789695.n4.nabble.com/Cbind-query-tp3006988p3008195.html Sent from the R help mailing list archive at Nabble.com.
On 23.10.2010 08:28, karthicklakshman wrote:> > Hello Petr Pikal, > > Thanks for the suggestion. > > I am sorry for not being very clear in my mail. from your example, Its clear > to me now about how to turn factors to character values. > > I am working on large data sets, and the ultimate aim is to create > interaction network, so the characters are very important and your post is > of true value. > > Thanks for your inputs > Regards, > karthickWell, others could perhaps help further on if you cited the original messages. Now it is really obfuscated what you actually asked and what the first answer was.... Best, Uwe Ligges