-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.

1

1 Answer 1

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")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.