Reputation: 533
I'm trying to reorder the elements in a list based on a partial match with a string vector. So that if I had the following list:
myList = list(BvsAadditionalinfo, AvsAothertext,
BvsBothertext, AvsBothertext, AvsBadditionalinfo,
BvsAothertext, BvsBadditionalinfo, AvsAadditionalinfo)
And a vector such as this:
vsList = c("AvsA", "AvsB", "BvsA", "BvsB")
I could sort my list so that I ended up with:
myList = list(AvsAothertext, AvsAadditionalinfo,
AvsBothertext, AvsBadditionalinfo, BvsAothertext,
BvsAadditionalinfo, BvsBothertext, BvsBadditionalinfo)
I've tried using things along the lines of order
:
TempFileList <- [order(match(myList, vsList))]
But it doesn't appear to work, presumably because the match isn't 100%. How can I tell it to accept a partial match?
I also don't have any attachment to my vsList, so I could just as easily use something like this:
abbreviations = c("A", "B")
If it would work.
Upvotes: 1
Views: 581
Reputation: 32548
myList = list(BvsAadditionalinfo = 0, AvsAothertext = 0,
BvsBothertext = 0, AvsBothertext = 0,
AvsBadditionalinfo = 0, BvsAothertext = 0,
BvsBadditionalinfo = 0, AvsAadditionalinfo = 0)
myList[order(-rowSums(sapply(seq_along(vsList), function(i)
i * grepl(vsList[i], names(myList)))), names(myList), decreasing = TRUE)]
#$`AvsAothertext`
#[1] 0
#$AvsAadditionalinfo
#[1] 0
#$AvsBothertext
#[1] 0
#$AvsBadditionalinfo
#[1] 0
#$BvsAothertext
#[1] 0
#$BvsAadditionalinfo
#[1] 0
#$BvsBothertext
#[1] 0
#$BvsBadditionalinfo
#[1] 0
Upvotes: 2