Home > front end >  Is there an R function to subset multiple data that uses numbers and letters in one column?
Is there an R function to subset multiple data that uses numbers and letters in one column?

Time:01-27

I am trying to filter data ("E11719","E11710","E11718",...) from an object with a column consisting of data with numbers and letters. How can I filter this?

df <- choose.files()
df <- read.csv(df)
df35 <- dplyr::filter(df$Name_Abbr, "E11907","E12018","E12001","E12000")

Error in UseMethod("filter") : no applicable method for 'filter' applied to an object of class "character"

df

Example of the first row:

   Name                                                  Name_G  Name_Abbr 
1  BetaCoV.bat.China.Rhinolophus_blythi.PrC31.MW703458   Gen1    B1       bat

Country.Species Continents   Longitude   Latitude  Color
bat             Bat          #N/A        #N/A      red

CodePudding user response:

The format of your filter() function is not correct. The first argument should be .data, which is a dataframe, but not a vector (df$Name_Abbr will return a vector).

I'm not sure which genes do you wish to include. If you wish to include all genes that starts with E1, you can do the following:

df %>% filter(grepl("^E1", Name_Abbr))

or

filter(df, grepl("^E1", Name_Abbr))

grepl() returns logical values, so grepl("^E1", Name_Abbr) will look for anything that starts (regex ^) with E1 in the Name_Abbr column.

  •  Tags:  
  • Related