Hi all: Im kind of new on R and I need help changing a table. The thing is, i read a file on R using the read.table command and the table looks like this: Item 3d Plot XY plot 001 1 0 001 0 1 001 0 1 002 1 0 002 1 0 002 0 1 ... .. .. And what I want to do is generate a new table by item with the sum of the numbres, the next one is an example: Item 3d Plot XY plot 001 1 2 002 2 1 003 ... ... Does anyone know how to do this? Thanks in advance, help is greatly appreciated -- View this message in context: http://www.nabble.com/Need-help-on-changing-a-table-tf4929014.html#a14107837 Sent from the R help mailing list archive at Nabble.com.
Note that read.table() reads a table and returns a _data frame_. So it is the _data frame_ that you want to change (a table is something else in R). This is a simple application of aggregate(), e.g.> aggregate(z[-1], list(Item=z$Item), sum)Item X3d_Plot XY_plot 1 1 1 2 2 2 2 1 where I got z from read.table("clipboard", header=TRUE). On Sat, 1 Dec 2007, gsmkb86 wrote:> > Hi all: > Im kind of new on R and I need help changing a table. The thing is, i read a > file on R using the read.table command and the table looks like this: > Item 3d Plot XY plot > 001 1 0 > 001 0 1 > 001 0 1 > 002 1 0 > 002 1 0 > 002 0 1 > ... .. .. > And what I want to do is generate a new table by item with the sum of the > numbres, the next one is an example: > > Item 3d Plot XY plot > 001 1 2 > 002 2 1 > 003 ... ... > > Does anyone know how to do this? Thanks in advance, help is greatly > appreciated >-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
That's no table that's a data.frame :). For what you want probably the easiest way is aggregate. Type ?aggregate for the help information Based on your example and assuming the data is called "mydata" this shold do what you want. aggregate(mydata[,2:3], by=list(item=mydata$Item), sum) --- gsmkb86 <gsmkb_86 at hotmail.com> wrote:> > Hi all: > Im kind of new on R and I need help changing a > table. The thing is, i read a > file on R using the read.table command and the table > looks like this: > Item 3d Plot XY plot > 001 1 0 > 001 0 1 > 001 0 1 > 002 1 0 > 002 1 0 > 002 0 1 > ... .. .. > And what I want to do is generate a new table by > item with the sum of the > numbres, the next one is an example: > > Item 3d Plot XY plot > 001 1 2 > 002 2 1 > 003 ... ... > > Does anyone know how to do this? Thanks in advance, > help is greatly > appreciated