[R] how to select all columns that contain in any of their rows a partial match for a string?

Rui Barradas ru|pb@rr@d@@ @end|ng |rom @@po@pt
Sat Oct 5 21:24:12 CEST 2019


Try the following

cols <- sapply(tot, function(x) any(grepl("^E94", x)))

To have the column numbers,


Hope this helps,

Rui Barradas

Às 19:50 de 05/10/19, Ana Marija escreveu:
> Hello,
> I have a data frame tot which has many columns and many rows.
> I am trying to find all columns that have say a value in any of their
> rows that STARTS WITH: "E94"
> for example there are columns like this:
>> unique(tot$diagnoses_icd9_f41271_0_44)
> [1] NA      "E9420"
> I tried:
> s=select(tot,starts_with("E94"))
> but this didn't return me anything. Data type in those columns is character.
> Thanks
> Ana
> ______________________________________________
> R-help using 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.html
> and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list