[R] test if elements of a character vector contain letters

Yihui Xie xie at yihui.name
Mon Aug 6 22:38:12 CEST 2012


You probably mean grepl('[a-zA-Z]', x)

Regards,
Yihui
--
Yihui Xie <xieyihui at gmail.com>
Phone: 515-294-2465 Web: http://yihui.name
Department of Statistics, Iowa State University
2215 Snedecor Hall, Ames, IA


On Mon, Aug 6, 2012 at 3:29 PM, Liviu Andronic <landronimirc at gmail.com> wrote:
> On Mon, Aug 6, 2012 at 6:42 PM, Bert Gunter <gunter.berton at gene.com> wrote:
>> nzchar(x) & !is.na(x)
>>
>> No?
>>
>
> It doesn't work for what I need:
>> x
>  [1] "a10" "b8"  "c9"  "d2"  "e3"  "f4"  "g1"  "h7"  "i6"  "j5"  "k"
> "l"   "m"   "n"
> [15] "o"   "p"   "q"   "r"   "s"   "t"   "u"   "v"   "w"   "x"   "y"
> "z"   "1"   "2"
> [29] "3"   "4"   "5"   "6"   "7"   "8"   "9"   "10"  "11"  "12"  "13"
> "14"  "15"  "16"
> [43] "17"  "18"  "19"  "20"  "21"  "22"  "23"  "24"  "25"  "26"
>> nzchar(x) & !is.na(x)
>  [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
> TRUE TRUE TRUE TRUE
> [18] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
> TRUE TRUE TRUE TRUE
> [35] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
> TRUE TRUE TRUE TRUE
> [52] TRUE
>
>
> I need to have TRUE when an element contains a letter, and FALSE when
> an element contains only numbers. The above returns TRUE for the
> entire vector.
>
> Regards
> Liviu



More information about the R-help mailing list