2017-10-23 1 views

Antwort

4

Sie tun können

sum(startsWith(rownames(mtcars), "M")) 
# [1] 10 

Andere, weniger effiziente Möglichkeiten sind

sum(grepl("^M", rownames(mtcars))) 
# [1] 10 
length(grep("^M", rownames(mtcars))) 
# [1] 10 
sum(regexpr("^M", rownames(mtcars)) == 1L) 
# [1] 10 
sum(substr(rownames(mtcars), 1, 1) == "M") 
# [1] 10 
0

Anothe r gute Möglichkeit, dies zu tun, ist mit Stringr-Paket

library(stringr) 
str_view(rownames(mtcars),'^M') # to see all results or 
str_view(rownames(mtcars),'^M', match = T) # to see only the match results 
Verwandte Themen