-1

I am new to R. I have a R dataframe of following structure:

     164_I_.CEL 164_II.CEL 183_I.CEL 183_II.CEL 2114_I.CEL
   1       4496       5310      4492       4511       2872
   2        181        280       137        101         91
   3       4556       5104      4379       4608       2972
   4        167        217        99         79         82
   5         89        110        69         58         47

I want to group the columns which have "_I.CEL" in the column name.

I need a list output like NI, NI, I, NI, I

where NI means Not I.

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
ghosh'.
  • 1,567
  • 1
  • 14
  • 19
  • Possible duplicate of [How to drop columns by name pattern in R?](https://stackoverflow.com/questions/15666226/how-to-drop-columns-by-name-pattern-in-r) – divibisan Mar 08 '19 at 01:19

1 Answers1

0

A combination of ifelse and grepl looking for the required pattern in the column names.

ifelse(grepl("_I\\.CEL", names(df1)), "I", "NI")
#[1] "NI" "NI" "I"  "NI" "I" 

where df1 is your data frame.

Or use fixed = TRUE

ifelse(grepl("_I.CEL", names(df1), fixed = TRUE), "I", "NI")
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213