Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R count character column

Tags:

r

I want to add a column containing the amount of letters a-z in a different column from the same row.

dataset$count <-length((gregexpr('[a-z]', as.character(dataset$text))[[1]]))

does not work.

The result I would like to acheive:

text  |  count
a     |  1
ao    |  2
ao2   |  2
as2e  |  3
as2eA |  3
like image 551
Chris Avatar asked Jun 17 '11 11:06

Chris


People also ask

How do I count the number of elements in a column in R?

We can use the length() function combined with double brackets to count the number of elements in a specific component of the list.


2 Answers

This should do the trick:

numchars<-function(txt){
  #basically your code, but to be applied to 1 item
  tmpres<-gregexpr('[a-z]', as.character(txt))[[1]]
  ifelse(tmpres[1]==-1, 0, length(tmpres))
}
#now apply it to all items:
dataset$count <-sapply(dataset$text, numchars)

Another option is more of a two-step approach:

charmatches<-gregexpr('[a-z]', as.character(dataset$text))[[1]]
dataset$count<-sapply(charmatches, length)
like image 21
Nick Sabbe Avatar answered Sep 18 '22 22:09

Nick Sabbe


Tricky one:

nchar(gsub("[^a-z]","",x))
like image 160
Marek Avatar answered Sep 17 '22 22:09

Marek