Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a function in R that allows to select rows where the row name ends in a lower case letter, and remove this letter?

Tags:

r

I have the following data.frame:

data <- as.data.frame(seq(1:5))
rownames(data) <- c("abc-2A","abc-2b", "def-3", "ACD4.54Y", "ghj-5c")
data
             seq(1:5)
abc-2A          1
abc-2b          2
def-3           3
ACD4.54Y        4
ghj-5c          5

I would like to do two things with this data frame:

  1. Make a subset of this data frame that contains only the rows where there row name ends with a lower case letter. This would be:

             seq(1:5)
    abc-2b          2
    ghj-5c          5
    
  2. Remove this lower case letter from the row names in the original data.frame. The final data.frame would be:

         seq(1:5)
    abc-2A          1
    abc-2           2
    def-3           3
    ACD4.54Y        4
    ghj-5           5
    

I have spent quite some time in stack overflow and google but I couldn't figure out how to do it. Does anyone have a suggestion?

Thanks a lot!

like image 425
Euclides Avatar asked Dec 13 '25 01:12

Euclides


1 Answers

you can use the dplyr::filterfunction to filter rows according to some criteria. Lower case letters at the end of a string can be identified with regular expressions like this [a-z]$. Combining all of this yields the following

library(dplyr)
  mydata <- data.frame(id = c("select thiss", "DONT SELECT"), x = 1:2) 

mydata %>% 
    filter(grepl("[a-z]$", id)) %>% 
    mutate(id = sub("[a-z]$", "", id))

           id x
1 select this 1
like image 186
Cettt Avatar answered Dec 14 '25 14:12

Cettt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!