Hello, I have been using the TM package to create a TermDocMatrix, which I have saved as a matrix so that I can view word frequencies. Below is a section of the code that I have used and an excerpt of the output: What I wanted to be able to do is to view the output alphabetically - rather than the results being sorted by frequency as below, that an alphabetical list would be generated. This would be quite long as the matrix is 176 x 2796, so I was hoping I could save the output in a csv file, which could be manipulated either in R or EXCEL. Any advice as to how I could do this is appreciated, Bob rec.matrix <- TermDocMatrix(recdata) recdata.matrix <- findFreqTerms(rec.matrix, 5, Inf) # creates a matrix new.matrix <- as(Data(rec.matrix), "matrix") tot <- colSums(new.matrix) sort(tot) unavail unwilling wheels 1 1 1 evans interviewed jill 1 1 1 referral achieved aids
Do you mean sort by the *names* of a *vector* alphabetically? (tot is a vector)? In which case tot[sort.list(names(tot))] If you want to sort new.matrix by its *colnames* of new.matrix, new.matrix[, sort.list(colnames(new.matrix))] See 'An Introduction to R' for the ideas involved, and the help pages of sort() and order(). On Sat, 18 Oct 2008, Bob Green wrote:> > Hello, > > I have been using the TM package to create a TermDocMatrix, which I have > saved as a matrix so that I can view word frequencies. Below is a section of > the code that I have used and an excerpt of the output: What I wanted to be > able to do is to view the output alphabetically - rather than the results > being sorted by frequency as below, that an alphabetical list would be > generated. This would be quite long as the matrix is 176 x 2796, so I was > hoping I could save the output in a csv file, which could be manipulated > either in R or EXCEL. > > Any advice as to how I could do this is appreciated, > > Bob > > rec.matrix <- TermDocMatrix(recdata) > recdata.matrix <- findFreqTerms(rec.matrix, 5, Inf) > > # creates a matrix > new.matrix <- as(Data(rec.matrix), "matrix") > tot <- colSums(new.matrix) > sort(tot) > > > unavail unwilling wheels > 1 1 1 > evans interviewed jill > 1 1 1 > referral achieved aids-- 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
Prof Ripley referred you to the help pages for sort and order for the sorting part of your question. For viewing large data objects look at View (note capitol V), head, tail, page, and write.csv/write.table. Hope this helps, -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Bob Green > Sent: Saturday, October 18, 2008 3:59 AM > To: r-help at r-project.org > Subject: [R] sorting matrix output alphabetically > > > Hello, > > I have been using the TM package to create a TermDocMatrix, which I > have saved as a matrix so that I can view word frequencies. Below is > a section of the code that I have used and an excerpt of the output: > What I wanted to be able to do is to view the output alphabetically - > rather than the results being sorted by frequency as below, that an > alphabetical list would be generated. This would be quite long as the > matrix is 176 x 2796, so I was hoping I could save the output in a > csv file, which could be manipulated either in R or EXCEL. > > Any advice as to how I could do this is appreciated, > > Bob > > rec.matrix <- TermDocMatrix(recdata) > recdata.matrix <- findFreqTerms(rec.matrix, 5, Inf) > > # creates a matrix > new.matrix <- as(Data(rec.matrix), "matrix") > tot <- colSums(new.matrix) > sort(tot) > > > unavail unwilling wheels > 1 1 1 > evans interviewed jill > 1 1 1 > referral achieved aids > > ______________________________________________ > 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.