[R] Generating Frequency Values
Sarah Goslee
sarah.goslee at gmail.com
Tue Nov 26 18:41:44 CET 2013
Hi,
If I understand the question don't you simply want:
> with(df.1, rep(Piglets, times=Frequency))
[1] 5 7 7 8 8 8 9 9 9 10 10 10 10 10 10 10 10 10 11 11 11 11 11 11 11
[26] 11 12 12 12 12 12 13 13 13 14 14
Sarah
On Tue, Nov 26, 2013 at 9:59 AM, Burhan ul haq <ulhaqz at gmail.com> wrote:
> Hi,
>
> My problem is as follows:
>
> INPUT:
> "Frequency" from one column and value of "Piglets" from another one
>
> OUTPUT:
> Repeat this "Piglet" value as per the "Frequency"
> i.e.
> Piglet 1, Frequency 3, implies 1,1,1
> Piglet 7, Frequency 2, implies 7,7
>
> SOLUTION:
> This is what I have tried so far:
>
> 1. A helper function:
>> dput(fn.1)
> function (df.1, vt.1)
> {
> i = c(1)
> for (i in seq_along(dim(df.1)[1])) {
> print(i)
> temp = rep(df.1$Piglets[i], df.1$Frequency[i])
> append(vt.1, values = temp)
> }
> }
>
> 2. A dummy data frame:
>> dput(df.1)
> structure(list(Piglets = 5:14, Frequency = c(1L, 0L, 2L, 3L,
> 3L, 9L, 8L, 5L, 3L, 2L)), .Names = c("Piglets", "Frequency"), class =
> "data.frame", row.names = c(NA,
> -10L))
>
> 3. A dummy vector to hold results:
>> dput(vt.1)
> 1
>
> 4. Finally the function call:
> fn.1(df.1, vt.1)
>
> 5. The results is:
> [1] 1
>
> PROBLEM:
> The result is not a repetition of Piglet value as per their respective
> frequencies.
>
>
>
> Thanks in advance for guidance and help.
>
> CheeRs !
>
>
> p.s I have used caps for my heading / sections, nothing else is implied by
> their use.
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
--
Sarah Goslee
http://www.stringpage.com
http://www.sarahgoslee.com
http://www.functionaldiversity.org
More information about the R-help
mailing list