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]]
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
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.
On 26-11-2013, at 15:59, 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) > } > } >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 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.