jonathanbriggs
2009-Jun-07 17:26 UTC
[R] How do I construct a one matrix from another? (newbie)
Apologies if this is an obvious question but I am teaching myself R and the occasional push in the right direction is much appreciated? I have a data.frame containing data that looks like this CustID itemID 1 a 1 b 2 c 3 a 3 c I want to assemble the vector of all the items bought by a particular customer and can't quite see how to do it. What is the best way to represent the answers for a single customer and by all customers? Thanks. -- View this message in context: http://www.nabble.com/How-do-I-construct-a-one-matrix-from-another--%28newbie%29-tp23913248p23913248.html Sent from the R help mailing list archive at Nabble.com.
Dylan Beaudette
2009-Jun-07 18:37 UTC
[R] How do I construct a one matrix from another? (newbie)
Hi, some ideas: ?split ?by library(plyr) ?ddply Cheers, Dylan On Sun, Jun 7, 2009 at 10:26 AM, jonathanbriggs<jonathanbriggs at mac.com> wrote:> > Apologies if this is an obvious question but I am teaching myself R and the > occasional push in the right direction is much appreciated? > > I have a data.frame containing data that looks like this > > CustID itemID > 1 ? ? ? ?a > 1 ? ? ? ?b > 2 ? ? ? ?c > 3 ? ? ? ?a > 3 ? ? ? ?c > > I want to assemble the vector of all the items bought by a particular > customer and can't quite see how to do it. > > What is the best way to represent the answers for a single customer and by > all customers? > > Thanks. > -- > View this message in context: http://www.nabble.com/How-do-I-construct-a-one-matrix-from-another--%28newbie%29-tp23913248p23913248.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. >
Jorge Ivan Velez
2009-Jun-07 18:51 UTC
[R] How do I construct a one matrix from another? (newbie)
Dear jonathanbriggs, Try x <- read.table(textConnection("CustID itemID 1 a 1 b 2 c 3 a 3 c"),header=TRUE) closeAllConnections() with(x, tapply(CustID, itemID, paste, collapse=",") ) # a b c # "1,3" "1" "2,3" with(x, tapply(itemID, CustID, paste, collapse=",") ) # 1 2 3 #"a,b" "c" "a,c" HTH, Jorge On Sun, Jun 7, 2009 at 1:26 PM, jonathanbriggs <jonathanbriggs@mac.com>wrote:> > Apologies if this is an obvious question but I am teaching myself R and the > occasional push in the right direction is much appreciated? > > I have a data.frame containing data that looks like this > > CustID itemID > 1 a > 1 b > 2 c > 3 a > 3 c > > I want to assemble the vector of all the items bought by a particular > customer and can't quite see how to do it. > > What is the best way to represent the answers for a single customer and by > all customers? > > Thanks. > -- > View this message in context: > http://www.nabble.com/How-do-I-construct-a-one-matrix-from-another--%28newbie%29-tp23913248p23913248.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]