Hi,

A big thanks to everyone who replied. But special ones to Berend for
pointing out my mistakes, that will really help me in future.



Cheers !


On Tue, Nov 26, 2013 at 11:19 PM, Berend Hasselman <b...@xs4all.nl> wrote:

>
> On 26-11-2013, at 15:59, Burhan ul haq <ulh...@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)
> >    }
> > }
> >
>
> There is a lot wrong with your function.
> You should assign the result of append to vt.1
> The function should return vt.1
> Use seq_len instead of seq_along.
>
> The function should be something like this
>
> fn.1 <- function (df.1, vt.1)
> {
>    for (i in seq_len(length.out=dim(df.1)[1])) {
>        print(i)
>        temp = rep(df.1$Piglets[i], df.1$Frequency[i])
>        vt.1 <- append(vt.1, values = temp)
>    }
>    vt.1
> }
>
> But Sarah’s solution is the way to go.
>
> Berend
>
>
> > 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@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.
>
>

        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

Reply via email to