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:
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
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!
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With