carol white
2016-Jan-24 18:24 UTC
[R] logical vector of the indices of a string in a vector
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]]
ruipbarradas at sapo.pt
2016-Jan-24 18:44 UTC
[R] logical vector of the indices of a string in a vector
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 > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > R-project.org/posting-guide.htmland provide commented, > minimal, self-contained, reproducible code.? [[alternative HTML version deleted]]