Hi everyone,
I am somewhat new to R and I am trying to write a permutation function such
that it inputs a character vector and from an arbitrary length "n"
which is
the length of the combinations for the character vector. I know there are R
packages for permutation but this is for an assignment.
So far this is what I have:
alphabet = c("a","b","c","d")
spot = c()
permute = function(alphabet,n){
for (i in 1:factorial(length(alphabet))){
perm = sample(alphabet, replace=F, size=n)
spot = rbind(spot, perm, deparse.level=2)
}
print(spot)
}
This function works but it has some flaws for what I need. I would like the
print output to have the rownames as the combination of the characters for
each row (ie aa for "a" "a"). Also, this code is producing
duplicate
combinations whereas I only want an output of unique combinations.
To address the rownames problem I have, I have been trying to meddle around
with creating a dataframe from rbind with something like:
data2 = data.frame(spot, check.names=TRUE)
I was thinking something along the lines of this to remove duplicates:
or something like make.unique(spot)
or make.names(spot, unique=TRUE)
Neither of these have been working for me. Could someone help point me in
the right direction?
Much appreciated.
--
View this message in context:
http://r.789695.n4.nabble.com/Writing-a-Permutation-Function-tp4594621p4594621.html
Sent from the R help mailing list archive at Nabble.com.
We really can't help you with your assignment. You might consider ??unique though, since you've already resolved to look for functions related to identifying unique entries. Sarah On Apr 28, 2012, at 8:11 AM, petermec <petermec at buffalo.edu> wrote:> Hi everyone, > > I am somewhat new to R and I am trying to write a permutation function such > that it inputs a character vector and from an arbitrary length "n" which is > the length of the combinations for the character vector. I know there are R > packages for permutation but this is for an assignment. > > So far this is what I have: > > alphabet = c("a","b","c","d") > spot = c() > permute = function(alphabet,n){ > for (i in 1:factorial(length(alphabet))){ > perm = sample(alphabet, replace=F, size=n) > spot = rbind(spot, perm, deparse.level=2) > } > print(spot) > } > > This function works but it has some flaws for what I need. I would like the > print output to have the rownames as the combination of the characters for > each row (ie aa for "a" "a"). Also, this code is producing duplicate > combinations whereas I only want an output of unique combinations. > > To address the rownames problem I have, I have been trying to meddle around > with creating a dataframe from rbind with something like: > data2 = data.frame(spot, check.names=TRUE) > > I was thinking something along the lines of this to remove duplicates: > or something like make.unique(spot) > or make.names(spot, unique=TRUE) > > Neither of these have been working for me. Could someone help point me in > the right direction? > > Much appreciated. > > > -- > View this message in context: http://r.789695.n4.nabble.com/Writing-a-Permutation-Function-tp4594621p4594621.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.
Ein eingebundener Text mit undefiniertem Zeichensatz wurde abgetrennt. Name: nicht verf?gbar URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20120428/d8d0d47b/attachment.pl>
Thanks for the input everyone!
So far this is the updated code that I have:
alphabet = c("a","b","c","d")
holder = c()
permute = function(alphabet,n){
for (i in 1:1000){
perm = sample(alphabet, replace=F, size=n)
holder = rbind(holder, perm, deparse.level=0)
}
data2 = unique(holder)
data3 = data2[order(data2[,1],data2[,2]),]
labels = apply(data3, 1, paste, sep="", collapse="")
print(data.frame(data3, row.names=labels))
}
Although the code seems to be working for me now, I feel that its still
rough and inherently wrong in some of its nature.
It is basically taking 1000 iterations of sample from the character string
input and from that 1000 iterations, only keeping the unique ones. What is
the character vector that is inputted ends up being 10 characters long, then
surely 1000 iterations wouldn't get all the unique values. Changing 1000
iterations to 1000000000 would be inefficient and CPU consuming.
Thanks again everyone.
--
View this message in context:
http://r.789695.n4.nabble.com/Writing-a-Permutation-Function-tp4594621p4594905.html
Sent from the R help mailing list archive at Nabble.com.