Skip to content
Prev 315975 / 398513 Next

extracting characters from a string

Hello,

I've just noticed that my first solution would only return the first set 
of alphabetic characters, such as "Van", not "Van den Hoops".
The following will solve that problem.


fun2 <- function(x, sep = ", "){
	x <- strsplit(x, sep)
	m <- lapply(x, function(y) gregexpr(" [[:alpha:]]*$", y))
	res <- lapply(seq_along(x), function(i)
		regmatches(x[[i]], m[[i]], invert = TRUE))
	res <- lapply(res, unlist)
	lapply(res, function(y) y[nchar(y) > 0])
}
fun2(pub)


Hope this helps,

Rui Barradas

Em 23-01-2013 18:33, Rui Barradas escreveu: