0

I was writing some code that need to inspect individual characters in a input string. I want to know whether there were numbers in string, and use strsplit to split the character into list. Then, use any() to check the numbers (the limitation is that I can't use grepl :(

b<-"Idontknow456"
b<-strsplit(b,"")
length(b)
any(6%in%b)

c<-list("d","t","5")
length(c)
any(5%in%c)

the result

  1
  FALSE
  3
  TRUE

May I ask how should I modify my code, so that I can split individual characters to inspect them?

  • You may need `unlist` or `[[1]]` to get a vector back i.e `b<-strsplit(b,"")[[1]]` – Ronak Shah Mar 15 '21 at 08:46
  • strsplit returns list, try `any("6" %in% unlist(b))` – zx8754 Mar 15 '21 at 08:46
  • But the solution is regex, see: https://stackoverflow.com/q/33393112/680068 – zx8754 Mar 15 '21 at 08:48
  • Maybe you can also try: `utf8ToInt("6") %in% utf8ToInt(b)` – GKi Mar 15 '21 at 09:12
  • but length() should return the number of items of a list , according to the doc I found ... :? https://www.google.com/search?q=length+of+a+list+in+r ; https://www.educative.io/answers/how-to-use-the-length-function-to-get-the-length-of-a-list-in-r So why doesn't it for the list returned by strsplit ? – Max Jun 06 '23 at 01:10

0 Answers0