[R] logical vector of the indices of a string in a vector

ruipbarradas at sapo.pt ruipbarradas at sapo.pt
Sun Jan 24 19:44:08 CET 2016


Hello,

Try

x[grepl("Yes", x) & x != ""]

Hope this helps,

Rui Barradas

 

Citando carol white via R-help <r-help at r-project.org>:

> Hi, it might be trivial but is there any way to get the logical  
> vector of the indices of a string in a vector? I thought that %in%  
> would do but it doesn't. I also want to filter the empty fields.
> Here I want to extract the non-empty elements containing "Yes":x  
> =c("Yes, fsd", "", "No","","Yes, fjsdlf", "")
> x[c("Yes") %in% x & x != ""]character(0)
>
> Above, I wanted to do the 2 following operations in 1. Here with  
> grep,  it works but %in% in above doesn't:y = x[grep("Yes", x)]
>> y = y[y != ""]
>> y
>
> [1] "Yes, fsd"    "Yes, fjsdlf"
> Thanks
> Carol
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide  
> http://www.R-project.org/posting-guide.htmland provide commented,  
> minimal, self-contained, reproducible code.

 

	[[alternative HTML version deleted]]



More information about the R-help mailing list