How do I subset data in R with a list of variable names?
1
0
Entering edit mode
24 months ago
hollie • 0

Hello! I have a table of results in R that include a column of bacterial species names. I have a separate list of species names that I want to use to only include data where one of these species is identified. I cannot find code to do this, does anybody know how to subset data in this way?

R • 1.9k views
ADD COMMENT
0
Entering edit mode

Would something like this work?

results %>% dplyr::filter(bacterial_species_names %in% species_names)
ADD REPLY
2
Entering edit mode
24 months ago

No need for tidyverse, this is simple in base R. Assuming your data.frame is named df and has a species column:

species <- c("human", "mouse")
df.sub <- df[df$species %in% species, ]
ADD COMMENT
0
Entering edit mode

thank you! this worked but now I have added the whole list, it comes back with 'Error:unexpected input in "names<-c(“"

names<-c(“Aeromonas hydrophila”, “Arcanobacterium haemolyticum”, “Bacillus”, “Citrobacter koseri”, “Citrobacter”, “Coagulase-negative Staphylococcus”, “Corynebacterium amycolatum”, “Corynebacterium aurimucosum”, “Corynebacterium propinquum”, “Corynebacterium striatum”, “Enterobacter cloacae”, “Enterobacteriaceae”, “Enterococcus faecalis”, “Enterococcus faecium”, “Escherichia coli”, “Finegoldia magna”, “Gemella morbillorum”, “Granulicatella adiacens”, “Klebsiella oxytoca”, “Klebsiella pneumoniae”, “Micrococcus luteus”, “Morganella morganii”, “Mycobacterium fortuitum”, “Propionibacterium acnes”, “Propionibacterium”, “Proteus mirabilis”, “Pseudomonas aeruginosa”, “Serratia marcescens”, “Staphylococcus aureus”, “Staphylococcus condimenti”, “Staphylococcus epidermidis”, “Staphylococcus lugdunensis”, “Streptococcus agalactiae”, “Streptococcus dysgalactiae”, “Streptococcus oralis”, “Streptococcus pneumoniae”, “Staphylococcus warneri”, “Staphylococcus capitis”, “Staphylococcus hominis”, “Streptococcus viridans”, “Prevotella”, “Enterobacter”, “Streptococcus mitis”, “Streptococcus milleri”, “Streptococcus pyogenes”, “Bacteroides fragilis”, “Tropheryma whipplei”, “Ureaplasma urealyticum”, “Shigella sonnei”, “Eschericha fergusonii”, “Neisseria meningitidis”, “Parvimonas micra”, “Enterococcus cecorum”, “Fusobacterium necrophorum”, “Shigella”, “Staphylococcus heamolyticusts”, “Staphylococcus caprae”, “Staphylococcus arlette”, “Staphylococcus capitis”, “Leuconostoc mesenteroides”, “Acinetobacter baumannii”, “Pseudomonas aeriginosa”, “Propionibacterium sp. H456”, “Klebsiella pneumoniae”, “Treponema pallidum"

ADD REPLY
0
Entering edit mode

I expect you're missing a comma somewhere.

Also, names is a useful function in R that you're overwriting here, so you may want to reconsider your variable name.

ADD REPLY
0
Entering edit mode

This could be a copy and paste error, but you also forgot the closing parenthesis, which might give the error you described.

ADD REPLY

Login before adding your answer.

Traffic: 1878 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6