[R] convert a table
David L Carlson
dcarlson at tamu.edu
Fri Jul 6 17:52:23 CEST 2012
A table is a matrix
Filename <- sample(LETTERS[1:5], 50, replace=TRUE)
PREDICT <- sample(1:7, 50, replace=TRUE)
To get a data frame that preserves the row/column structure of the table use
dfm <- as.data.frame.matrix(tbl)
But you can do what you want on the table directly:
rowmx <- apply(tbl, 1, max)
newtbl <- apply(tbl, 2, function(x) ifelse(x<rowmx, 0, x))
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77843-4352
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Amanduh320
> Sent: Friday, July 06, 2012 10:03 AM
> To: r-help at r-project.org
> Subject: [R] convert a table
> I have my data in a table
> table <- table(test2$Filename, test2$PREDICT)
> I need to convert this table so it keeps the same structure, but is a
> different format. The current output is count data by Filename and I
> want to
> get the max for each Filename.
> Columns are:
> Filename, 1, 2, 3, 4, 5, 6, 7
> When I try to convert it to a data.frame it reverts to Var1(Filename),
> Var2(1:7), Freq.
> My end goal is to find the max by row (Filename), then do ifelse(x<max,
> max) for each value in columns 2:8
> My problem is that I don't understand what format the table is in.
> Thank you!
> View this message in context: http://r.789695.n4.nabble.com/convert-a-
> Sent from the R help mailing list archive at Nabble.com.
> R-help at r-project.org mailing list
> PLEASE do read the posting guide http://www.R-project.org/posting-
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help