On Sun, 17 Sep 2006, jim holtman wrote:

You can use 'names'

d = as.data.frame(matrix(c(1, 2, 3, 3), 2,2))

x <- table(unlist(d))
x

1 2 3
1 1 2
names(x)
[1] "1" "2" "3"

You can, but Søren was equally correct: names() on a 1D array actually looks up the dimnames. OTOH, we do really want to encourage people to use the accessor functions, so

dimnames(x)[[1]]
[1] "1" "2" "3"

is better supported.


On 9/17/06, Søren Merser <[EMAIL PROTECTED]> wrote:
hi, here's a way:
attr(table(unlist(d)),'dimnames')[[1]]
[1] "1" "2" "3"
as.numeric(table(unlist(d)))
[1] 1 1 2

You don't need as.numeric here: as.vector will remove the attributes if that is the intention.


soren

----- Original Message -----
From: "Bingshan Li" <[EMAIL PROTECTED]>
To: "jim holtman" <[EMAIL PROTECTED]>
Cc: <[email protected]>
Sent: Sunday, September 17, 2006 6:14 AM
Subject: Re: [R] using "table" in R


Hi Jim,

This is the way to get the frequencies. But what I
want is to store the elements in one vector and their
frequencies in another vector. My problem is that when
I call "table" to return the frequency table, I do not
know how to extract these two vectors. I tried
table(...)$dinnames and it did not work. It returned
NULL.

Thanks!


--- jim holtman <[EMAIL PROTECTED]> wrote:

Here is one way; you create a vector of the data in
the dataframe with
'unlist' and then use table:

d = as.data.frame(matrix(c(1, 2, 3, 3), 2,2))
d
  V1 V2
1  1  3
2  2  3
table(unlist(d))

1 2 3
1 1 2



On 9/16/06, Bingshan Li <[EMAIL PROTECTED]>
wrote:
Hi there,

I have a dataframe whose elements are numbers or
characters. I want to extract the frequencies of
each
elements in the dataframe. For example,

d = as.data.frame(matrix(c(1, 2, 3, 3), 2,2))

What I want is first what are the elements in the
data
(1,2,3 here) and second what are their frequencies
(1,1,2 respectively). How to use "table" to
extract
these two pieces of information? I played with
"table"
but couldn't extract the information. Please
assume
that we do not know how many elements in the
dataframe
a priori.

Thanks a lot!

______________________________________________
[email protected] 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 you are trying to solve?


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

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





--
Brian D. Ripley,                  [EMAIL PROTECTED]
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
______________________________________________
[email protected] 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 via email to