Re: [R] disaggregate frequency table into flat file

From: Marc Schwartz <marc_schwartz_at_comcast.net>
Date: Thu, 22 May 2008 09:04:43 -0500

Is this what you want?

 > xtabs(Freq ~ Var1 + Var2, data = orig)

     Var2
Var1 A B

    A 40 30
    B 5 25

See ?xtabs

Or is this what you want?

expand.dft <- function(x, na.strings = "NA", as.is = FALSE, dec = ".") {

   DF <- sapply(1:nrow(x), function(i) x[rep(i, each = x$Freq[i]), ],

                simplify = FALSE)

   DF <- subset(do.call("rbind", DF), select = -Freq)

   for (i in 1:ncol(DF))
   {

     DF[[i]] <- type.convert(as.character(DF[[i]]),
                             na.strings = na.strings,
                             as.is = as.is, dec = dec)

   }

   DF
}

DF <- expand.dft(orig)

 > str(DF)
'data.frame': 100 obs. of 2 variables:
  $ Var1: Factor w/ 2 levels "A","B": 1 1 1 1 1 1 1 1 1 1 ...   $ Var2: Factor w/ 2 levels "A","B": 1 1 1 1 1 1 1 1 1 1 ...

HTH, Marc Schwartz

on 05/22/2008 07:56 AM maiya wrote:
> sorry, my mistake!
> the data frame should read:
> orig<-as.data.frame.table(orig)
> orig
> Var1 Var2 Freq
> 1 A A 40
> 2 B A 5
> 3 A B 30
> 4 B B 25
>
> but basicaly i would simply like a sample of the original matrix ( which is
> a frequency table/contingency table/crosstabulation)
>
> hope this is clearer now!
>
> maja
>
>
>
>
>
>
> jholtman wrote:

>> Not exactly clear what you are asking for.  Your data.frame.table does not
>> seem related to the original 'orig'.  What exactly are you expecting as
>> output?
>>
>> On Wed, May 21, 2008 at 10:16 PM, maiya <maja.zaloznik_at_gmail.com> wrote:
>>
>>> i appologise for the trivialness of this post - but i've been searching
>>> the
>>> forum wothout luck - probably simply because it's late and my brain is
>>> starting to go..
>>>
>>> i have a frequency table as a matrix:
>>>
>>> orig<-matrix(c(40,5,30,25), c(2,2))
>>> orig
>>>     [,1] [,2]
>>> [1,]   40   30
>>> [2,]    5   25
>>>
>>> i basically need a random sample say 10 from 100:
>>>
>>>     [,1] [,2]
>>> [1,]   5   2
>>> [2,]    0   3
>>>
>>> i got as far as
>>>
>>> orig<-as.data.frame.table(orig)
>>> orig
>>>  Var1 Var2 Freq
>>> 1    A    A   10
>>> 2    B    A    5
>>> 3    A    B   30
>>> 4    B    B   25
>>>
>>> and then perhaps
>>>
>>> individ<-rep(1:4, times=orig$Freq)
>>>
>>> which gives a vector of the 100 individuals in each of the 4 groups -
>>> cells,
>>> but I'm
>>> (a) stuck here and
>>> (b) afraid this is a very round-about way at getting to what I want i.e.
>>> I
>>> can now sample(individ, 10), but then I'll have a heck of a time getting
>>> the
>>> result back into the original matrix form....
>>>
>>> sorry again, just please tell me the simple solution that I've missed?
>>>
>>> thanks!
>>>
>>> maja
>>>
>

______________________________________________
R-help_at_r-project.org 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. Received on Thu 22 May 2008 - 14:09:45 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 22 May 2008 - 17:30:40 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive