[R] Generating Frequency Values

arun smartpink111 at yahoo.com
Tue Nov 26 18:43:31 CET 2013


Hi,

Not sure whether this is what you wanted.
df.1[rep(1:nrow(df.1),df.1[,2]),]
A.K.




On Tuesday, November 26, 2013 12:31 PM, 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.




More information about the R-help mailing list