r - How do I determine the total number of cities that have less than seven characters? -



r - How do I determine the total number of cities that have less than seven characters? -

i using info frame swiss , determine total number of cities have less 7 characters. can done sum , nchar ?

thanks!

edit:

as @kfb noted, should clarify other issue , give example. might have not best example, can show meant in comments.

housing

so in info have column type , count how many values have less 7 characters sum , nchar command. same can done infl column.

you can try:

sum(nchar(rownames(swiss))<7) #[1] 16

counting specific column in data.frame (assuming nchar)

nchar(swiss$fertility) #[1] 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 2 4 4 4 4 2 4 4 4 4 4 4 4 4 2 4 4 #[39] 4 4 4 4 4 4 2 4 4

if want find length of each column, nrow(swiss) not going alter specific column.

update library(mass) data(housing) sapply(housing[,c("infl", "type")], function(x) sum(nchar(as.character(x))<7)) #infl type # 72 36

for 1 column, don't need sapply

sum(nchar(as.character(housing[,"infl"])) <7) #[1] 72

r

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -