convert a table

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

convert a table

Amanduh320
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, 0, max) for each value in columns 2:8

My problem is that I don't understand what format the table is in.
Thank you!
Amanda
Reply | Threaded
Open this post in threaded view
|

Re: convert a table

andrija djurovic
Hi. Here are some examples that could be useful here:

set.seed(1)
x <- sample(1:2, 100, rep=T)
y <- sample(1:3, 100, rep=T)
TAB <- table(x,y)

#check the object class

class(TAB)
apply(TAB, 2, max)
apply(TAB[,2:3], 2, max)
apply(TAB, 1, max)

DF <- as.data.frame.matrix(TAB)
class(DF)
sapply(DF, max)

Look at ?class, ?table, ?"[" , ?apply, ?as.data.frame.matrix and read
posting guide.

Andrija


On Fri, Jul 6, 2012 at 5:03 PM, Amanduh320 <[hidden email]> wrote:

> 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, 0,
> max) for each value in columns 2:8
>
> My problem is that I don't understand what format the table is in.
> Thank you!
> Amanda
>
> --
> View this message in context:
> http://r.789695.n4.nabble.com/convert-a-table-tp4635615.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: convert a table

David Carlson
In reply to this post by Amanduh320
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: [hidden email] [mailto:r-help-bounces@r-
> project.org] On Behalf Of Amanduh320
> Sent: Friday, July 06, 2012 10:03 AM
> To: [hidden email]
> 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,
> 0,
> max) for each value in columns 2:8
>
> My problem is that I don't understand what format the table is in.
> Thank you!
> Amanda
>
> --
> View this message in context: http://r.789695.n4.nabble.com/convert-a-
> table-tp4635615.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: convert a table

Amanduh320
Fantastic, thank  you so much!
Reply | Threaded
Open this post in threaded view
|

Re: convert a table

David Carlson
In reply to this post by David Carlson
I managed to leave out one key line:

Filename <- sample(LETTERS[1:5], 50, replace=TRUE)
PREDICT <- sample(1:7, 50, replace=TRUE)
tbl <- table(Filename, PREDICT)     # this one.

Then the rest follows.
----------------------------------------------
David

> -----Original Message-----
> From: [hidden email] [mailto:r-help-bounces@r-
> project.org] On Behalf Of David L Carlson
> Sent: Friday, July 06, 2012 10:52 AM
> To: 'Amanduh320'; [hidden email]
> Subject: Re: [R] convert a table
>
> 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: [hidden email] [mailto:r-help-bounces@r-
> > project.org] On Behalf Of Amanduh320
> > Sent: Friday, July 06, 2012 10:03 AM
> > To: [hidden email]
> > 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,
> > 0,
> > max) for each value in columns 2:8
> >
> > My problem is that I don't understand what format the table is in.
> > Thank you!
> > Amanda
> >
> > --
> > View this message in context: http://r.789695.n4.nabble.com/convert-
> a-
> > table-tp4635615.html
> > Sent from the R help mailing list archive at Nabble.com.
> >
> > ______________________________________________
> > [hidden email] 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.
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: convert a table

Jeff Newmiller
In reply to this post by Amanduh320
If you don't know what format your data is in, how are we supposed to know?

Have you tried looking at your data file with a text editor? Perhaps reading your data using read.table or read.csv will help? Use the str function to learn its structure? use dput as the posting guide recommends so we can look at it ourselves?
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<[hidden email]>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
---------------------------------------------------------------------------
Sent from my phone. Please excuse my brevity.

Amanduh320 <[hidden email]> wrote:

>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,
>0,
>max) for each value in columns 2:8
>
>My problem is that I don't understand what format the table is in.
>Thank you!
>Amanda
>
>--
>View this message in context:
>http://r.789695.n4.nabble.com/convert-a-table-tp4635615.html
>Sent from the R help mailing list archive at Nabble.com.
>
>______________________________________________
>[hidden email] 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.

______________________________________________
[hidden email] 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.