Good evening. I have this following table and I would like to turn it into a matrix in which my rows would be filled with de "Sellers", my columns with my "Articles" and my data would be the mean unitary price used by each seller in each produt. Seller Art. Unit Price 1 v1 p1 9.148352 2 v2 p1 2.858073 3 v3 p1 3.775315 4 v4 p1 9.821429 5 v3 p1 3.286827 6 v5 p2 11.105769 This is only the head of a table with more than 400.000 lines, 6.000 sellers and 4500 produts. Can anybody help me in suh a task considered very difficult by someone that has heard about R only a few hours? Thank you! SPBM -- View this message in context: http://www.nabble.com/Creating-a-matrix-tp19886510p19886510.html Sent from the R help mailing list archive at Nabble.com.
You can read it in as a data.frame and then use 'aggregate' to get the mean:> x <- read.table(textConnection(' Seller Art. Unit_Price+ 1 v1 p1 9.148352 + 2 v1 p1 2.858073 + 3 v1 p2 3.775315 + 4 v1 p2 9.821429 + 5 v1 p3 3.286827 + 6 v1 p3 11.105769') , header=TRUE)> closeAllConnections() > > xSeller Art. Unit_Price 1 v1 p1 9.148352 2 v1 p1 2.858073 3 v1 p2 3.775315 4 v1 p2 9.821429 5 v1 p3 3.286827 6 v1 p3 11.105769> aggregate(x$Unit_Price, list(x$Seller, x$Art.), mean)Group.1 Group.2 x 1 v1 p1 6.003213 2 v1 p2 6.798372 3 v1 p3 7.196298>On Wed, Oct 8, 2008 at 4:03 PM, SP&BM <sandra_peneda at hotmail.com> wrote:> > Good evening. > > I have this following table and I would like to turn it into a matrix in > which my rows would be filled with de "Sellers", my columns with my > "Articles" and my data would be the mean unitary price used by each seller > in each produt. > > Seller Art. Unit Price > 1 v1 p1 9.148352 > 2 v2 p1 2.858073 > 3 v3 p1 3.775315 > 4 v4 p1 9.821429 > 5 v3 p1 3.286827 > 6 v5 p2 11.105769 > > This is only the head of a table with more than 400.000 lines, 6.000 sellers > and 4500 produts. > Can anybody help me in suh a task considered very difficult by someone that > has heard about R only a few hours? > > Thank you! > SPBM > > -- > View this message in context: http://www.nabble.com/Creating-a-matrix-tp19886510p19886510.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?
?aggregate ( as Jim Holtman pointed out) is the standard way. Another way is to use the reshape package and try something like library(reshape) names(x)[3] <- "value" cast(x, Seller + Art. ~., mean) (Note the names(x)[3] <- "value" seems necessary at the moment as reshape only seems to work on a variable named value. I think this is supposed to change in the near future). --- On Wed, 10/8/08, SP&BM <sandra_peneda at hotmail.com> wrote:> From: SP&BM <sandra_peneda at hotmail.com> > Subject: [R] Creating a matrix > To: r-help at r-project.org > Received: Wednesday, October 8, 2008, 4:03 PM > Good evening. > > I have this following table and I would like to turn it > into a matrix in > which my rows would be filled with de "Sellers", > my columns with my > "Articles" and my data would be the mean unitary > price used by each seller > in each produt. > > Seller Art. Unit Price > 1 v1 p1 9.148352 > 2 v2 p1 2.858073 > 3 v3 p1 3.775315 > 4 v4 p1 9.821429 > 5 v3 p1 3.286827 > 6 v5 p2 11.105769 > > This is only the head of a table with more than 400.000 > lines, 6.000 sellers > and 4500 produts. > Can anybody help me in suh a task considered very difficult > by someone that > has heard about R only a few hours? > > Thank you! > SPBM > > -- > View this message in context: > http://www.nabble.com/Creating-a-matrix-tp19886510p19886510.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.__________________________________________________________________ [[elided Yahoo spam]]