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
We can use the length() function combined with double brackets to count the number of elements in a specific component of the list.
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)
Tricky one:
nchar(gsub("[^a-z]","",x))
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