Re: [R] Simple table with frequency variable

2007-08-03 Thread Johan Lindbäck
Ok, then tapply() might be your friend:

set.seed(1)
b - rbinom(10,6,0.3)
c - rbinom(10,6,0.9)
w - trunc(runif(10)*3)
b;c;w

table(b, c)
tapply(w, list(b, c), sum)

Is this what you were looking for?
/Johan



G. Draisma skrev:
 Thanks Johan,
 
 I realize that I did not use the right example.
 I have a table with two factors, say b and c,
 and a third case weight variable, say w.
 Then I would like the table command to sum the weights w
 for each combination of i and j.
 For instance, with
   b - rbinom(10,6,0.3)
   c - rbinom(10,6,0.9)
   w - trunc(runif(10)*3)
 the command
   table(i,j)
 counts the occurrences of combinations (b,c).
 I am looking for a command like
   table(i,j,weight=w)
 that would print the sum of the w's for each combination.
 e.g. if the combination (2,5) occurs twice
 I would like to see the sum of the two weights
 in the table, instead of the count 2.
 
 Gerrit.
 
 
 
 
 on 02-Aug-2007 11:03 Johan Lindbäck said the following:
 Would it be ok with a matrix?

 i - 1:5; j - 1:2

 li - length(i)
 lj - length(j)
 A - matrix(numeric(li * lj), nrow = li, dimnames = list(i, j))
 for (r in 1:li)
   for (s in 1:lj)
 A[r, s] - 10*r + s
 A

 HTH
 /Johan



 G. Draisma skrev:
 Thank you Jim,
 Sorry, that I was not clear enough.
 Each case has a frequency variable N.
 so when tabulating combinations (i,j) they
 should be weighted with weight N.

 In this case I would like a command
 table(i,j,N)
 resulting in
  j
   i   1  2
 1 11 12
 2 21 22
 ...
 5 51 52

 And I was looking for a table command
 that allows for a case weight variable.
 Gerrit.


 on 01-Aug-2007 22:38 jim holtman said the following:
 I am not exactly sure what you are asking for.  I am assuming that you
 want a vector that represent the combinations that are given
 combinations that are present:

 N
  [1] 11 22 31 42 51 12 21 32 41 52
 table(i,j)
j
 i   1 2
   1 1 1
   2 1 1
   3 1 1
   4 1 1
   5 1 1
 z - table(i,j)
 which(z==1)
  [1]  1  2  3  4  5  6  7  8  9 10
 which(z==1,arr.ind=T)
   row col
 1   1   1
 2   2   1
 3   3   1
 4   4   1
 5   5   1
 1   1   2
 2   2   2
 3   3   2
 4   4   2
 5   5   2
 x - which(z==1,arr.ind=T)
 paste(rownames(z)[x[,'row']], colnames(z)[x[,'col']], sep='')
  [1] 11 21 31 41 51 12 22 32 42 52


 On 8/1/07, G. Draisma [EMAIL PROTECTED] wrote:
 Hallo,

 Im trying to find out how to tabulate frequencies
 of factors when the data have a frequency variable.

 e,g:
 i-rep(1:5,2)
 j-rep(1:2,5)
 N-10*i+j

 table(i,j) gives a table of ones
 as each combination occurs only once.
 How does one get a table with the corresponding N's?

 Thanks!
 Gerrit.


 -- 
 Gerrit Draisma
 Department of Public Health
 Erasmus MC, University Medical Center Rotterdam
 Room AE-103
 P.O. Box 2040 3000 CA  Rotterdam The Netherlands
 Phone: +31 10 4087124 Fax: +31 10 4638474
 http://mgzlx4.erasmusmc.nl/pwp/?gdraisma

 __
 R-help@stat.math.ethz.ch 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.


 

-- 
  O|  Johan Lindbäck,  Biostatistician
 /\|   Uppsala Clinical Research Center
 | \_. SE-751 85  UPPSALA
~~ http://www.ucr.uu.se --

__
R-help@stat.math.ethz.ch 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.


Re: [R] Simple table with frequency variable

2007-08-02 Thread Johan Lindbäck
Would it be ok with a matrix?

i - 1:5; j - 1:2

li - length(i)
lj - length(j)
A - matrix(numeric(li * lj), nrow = li, dimnames = list(i, j))
for (r in 1:li)
  for (s in 1:lj)
A[r, s] - 10*r + s
A

HTH
/Johan



G. Draisma skrev:
 Thank you Jim,
 Sorry, that I was not clear enough.
 Each case has a frequency variable N.
 so when tabulating combinations (i,j) they
 should be weighted with weight N.
 
 In this case I would like a command
 table(i,j,N)
 resulting in
  j
   i   1  2
 1 11 12
 2 21 22
 ...
 5 51 52
 
 And I was looking for a table command
 that allows for a case weight variable.
 Gerrit.
 
 
 on 01-Aug-2007 22:38 jim holtman said the following:
 I am not exactly sure what you are asking for.  I am assuming that you
 want a vector that represent the combinations that are given
 combinations that are present:

 N
  [1] 11 22 31 42 51 12 21 32 41 52
 table(i,j)
j
 i   1 2
   1 1 1
   2 1 1
   3 1 1
   4 1 1
   5 1 1
 z - table(i,j)
 which(z==1)
  [1]  1  2  3  4  5  6  7  8  9 10
 which(z==1,arr.ind=T)
   row col
 1   1   1
 2   2   1
 3   3   1
 4   4   1
 5   5   1
 1   1   2
 2   2   2
 3   3   2
 4   4   2
 5   5   2
 x - which(z==1,arr.ind=T)
 paste(rownames(z)[x[,'row']], colnames(z)[x[,'col']], sep='')
  [1] 11 21 31 41 51 12 22 32 42 52


 On 8/1/07, G. Draisma [EMAIL PROTECTED] wrote:
 Hallo,

 Im trying to find out how to tabulate frequencies
 of factors when the data have a frequency variable.

 e,g:
 i-rep(1:5,2)
 j-rep(1:2,5)
 N-10*i+j

 table(i,j) gives a table of ones
 as each combination occurs only once.
 How does one get a table with the corresponding N's?

 Thanks!
 Gerrit.


 --
 Gerrit Draisma
 Department of Public Health
 Erasmus MC, University Medical Center Rotterdam
 Room AE-103
 P.O. Box 2040 3000 CA  Rotterdam The Netherlands
 Phone: +31 10 4087124 Fax: +31 10 4638474
 http://mgzlx4.erasmusmc.nl/pwp/?gdraisma

 __
 R-help@stat.math.ethz.ch 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.


 

-- 
  O|  Johan Lindbäck,  Biostatistician
 /\|   Uppsala Clinical Research Center
 | \_. SE-751 85  UPPSALA
~~ http://www.ucr.uu.se --

__
R-help@stat.math.ethz.ch 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.


Re: [R] labels attached to variable names

2005-01-06 Thread Johan Lindbäck
Denis Chabot wrote:
Hi,
Can we attach a more descriptive label (I may use the wrong  
terminology, which would explain why I found nothing on the FAQ) to  
variable names, and later have an easy way to switch to these labels in  
plots? I fear this is not possible and one must enter this by hand as  
ylab and xlab when making plots.

Thanks in advance,
Denis Chabot

Try
library(Hmisc)
?label
/Johan
--
No virus found in this outgoing message.
Checked by AVG Anti-Virus.
__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html