[R] functions of dataframes

Denham Robert Robert.Denham at nrm.qld.gov.au
Mon Jul 1 06:09:16 CEST 2002


I think what you want is to apply scramble to each column.  You can do
this with apply:

apply(dataset,2,scramble)

which gives me:

     p14 p88 p82 p90 p98
[1,]  36  40  43  11  17
[2,]  16  21   2  11   1
[3,]  16  40   4  50  19


Robert


-----Original Message-----
From: Robin Hankin [mailto:r.hankin at auckland.ac.nz]
Sent: Monday, 1 July 2002 12:16 PM
To: r-help at stat.math.ethz.ch
Subject: [R] functions of dataframes


Hello experts

I have a thing which I can't vectorize.  Can any gurus out there help?
This is my truncated dataset; it is a dataframe of five possums and
which tree they slept in on three days.

R> dataset
  p14 p88 p82 p90 p98
1   6   7   5   1   3
2   8   9  10   1  11
3   8   7  12  13  14

I can manipulate it with simple functions fine:

R> f
function(n){n+1000}

R> f(dataset)
   p14  p88  p82  p90  p98
1 1006 1007 1005 1001 1003
2 1008 1009 1010 1001 1011
3 1008 1007 1012 1013 1014

Great.  But I want to permute the trees as part of a Monte Carlo
simulation:

R> scramble
function(n){sample(50)[n]}

Hence
R> scramble(c(1,2,2,2,2,2,3))
[1] 31 26 26 26 26 26  4

preserves the fact that this (imaginary) possum slept in the same tree
most of the time.

PROBLEM:  scramble() doesn't work on the dataframe:

R> scramble(dataset)
Error in scramble(dataset) : invalid subscript type

I can convert it to a matrix but then I lose the structure:

R> scramble(as.matrix(dataset))
 [1] 44 24 24  8 23  8 29 10 19 16 16  7 11 15 30

How do I get answers like f(dataset) for scramble(dataset)?





R> dput(dataset)
structure(list(p14 = c(6, 8, 8), p88 = c(7, 9, 7), p82 = c(5, 
10, 12), p90 = c(1, 1, 13), p98 = c(3, 11, 14)), .Names = c("p14", 
"p88", "p82", "p90", "p98"), row.names = c("1", "2", "3"), class =
"data.frame")



************************************************************************
The information in this e-mail together with any attachments is
intended only for the person or entity to which it is addressed
and may contain confidential and/or privileged material.

Any form of review, disclosure, modification, distribution
and/or publication of this e-mail message is prohibited.  

If you have received this message in error, you are asked to
inform the sender as quickly as possible and delete this message
and any copies of this message from your computer and/or your
computer system network.  
************************************************************************

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list