[R] functions of dataframes

Robin Hankin r.hankin at auckland.ac.nz
Mon Jul 1 04:15:32 CEST 2002


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")



 

-- 

Robin Hankin, Lecturer,
School of Geographical and Environmental Science
Private Bag 92019 Auckland
New Zealand

r.hankin at auckland.ac.nz
tel 0064-9-373-7599 x6820; FAX 0064-9-373-7042
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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