Skip to Content

compare one list item against the rest in R

If you have a list of files, and you want to compare 1 against a set of the others, how do you do it?

my.test <- list[1]
my.reference.set <- list[-1]

This works of course, but I want to have this in a loop, with my.test varying each time (so that each file in the list is my.test for one iteration i.e. I have a list of 250 files, and I want to do this for every subset of 12 files within it.

> num <- (1:2)
> sdasd<- c("asds", "ksad", "nasd", "ksasd", "nadsd", "kasdih")
> splitlist<- split(sdasd, num)
> splitlist
[1] "asds"  "nasd"  "nadsd"
[1] "ksad"   "ksasd"  "kasdih"
> for (i in splitlist) {my.test <- splitlist[i] # "asds"
+ my.reference.set <- splitlist[-i] # "nasd" and "nadsd"
+ combined <- data.frame (my.test, my.reference.set)
+ combined}
Error in -i : invalid argument to unary operator

then i want next iteration to be,

my.test <- splitlist[i] #my.test to be "nasd"
my.reference.set <- splitlist[-i] # "asds" and "nadsd"

and finally for splitlist[1],

my.test <- splitlist[i] # "nadsd" 
my.reference.set <- splitlist[-i] # "asds" and "ksad"

Then the same for splitlist[2]