[R] Manipulating lists

hadley wickham
Mon, 17 Nov 2008 04:07:52 -0000

On Sun, Nov 16, 2008 at 9:56 PM, Christian Arnold <chrarnold at web.de>
wrote:
> Hello R-Community,
>
> I am pretty new to R and I am fascinated what R can do! I am doing
> phylogenetic analysis in R, and my current project includes two problems
> that I am unable to solve, unfortunately. I am hoping that someone knows an
> easy, and R-typical solution, any help would be appreciated. Specifically,
I
> have the following two problems:
>
> 1) I am reading in all the files from a directory using file_list <-
> list.files(). It works, of course, however, I want to restrict file_list to
> files that end with ".nex". Thus, I want to remove some files
from this
> list. I read that I can do something like file_list <-
file_list[condition].
> I do not know how to express my condition here, unfortunately. Pseudo code
> would be something like file_list <- file_list[ends with
".nex"]

Maybe you could use dir() instead - see particularly the pattern argument.

> 2) A slightly more complicated problem, at least I think that. I have two
> lists, list1 and list2. list1 contains some elements (characters such as
> "species1"). list2 contains also species names, including all of
list1, but
> also species that are not part of list1. I need a way to create a list that
> contains ONLY those elements that are in list2, but not in list1.
> For example: list1 =
c("s1","s3","s4","s7"), list2 >
c("s1","s2","s3","s4","s5","s6","s7"),
now I need a way to create the list
> list3 = c("s2","s5","s6"). Any ideas? I am
not able to find the appropriate
> function...

Have a look at setdiff.

Hadley


--