Reputation: 95
I have two lists of characters that i read in from excel files
One is a very long list of all bird species that have been documented in a region (allBirds
) and another is a list of species that were recently seen in a sample location (sampleBirds
), which is much shorter. I want to write a section of code that will compare the lists and tell me which sampleBirds
show up in the allBirds
list. Both are lists of characters.
I have tried:
# upload xlxs file
Full_table <- read_excel("Full_table.xlsx")
Pathogen_table <- read_excel("pathogens.xlsx")
# read species columnn into a new dataframe
species <-c(as.data.frame(Full_table[,7], drop=FALSE))
pathogens <- c(as.data.frame(Pathogen_table[,3], drop=FALSE))
intersect(pathogens, species)
intersect(species, pathogens)
but intersect is outputting lists of 0, which I know cannot be true, any suggestions?
Upvotes: 0
Views: 447
Reputation: 3234
You need to run the intersect on the individual columns that are stored in the list:
> a <- c(data.frame(c1=as.factor(c('a', 'q'))))
> b <- c(data.frame(c1=as.factor(c('w', 'a'))))
> intersect(a,b)
list()
> intersect(a$c1,b$c1)
[1] "a"
This will probably do in your case
intersect(Full_table[,7], Pathogen_table[,3])
Or if you insist on creating the data.frames:
intersect(pathogens[1,], species[1,])
where [1,]
should select the first column of the data.frame only. Note that by using c(as.data.frame(...
you are converting the data.frame
to a regular list. I'd go with only as.data.frame(...
.
Upvotes: 1