Ana Marija
2020-May-15 17:00 UTC
[R] how to extract strings in any column and in any row that start with
Hello, I have a data frame:> dim(tot)[1] 502536 1093 How would I extract from it all strings that start with E10? I know how to extract all rows that contain with E10 df0<-tot %>% filter_all(any_vars(. %in% c('E10')))> dim(df0)[1] 5105 1093 but I just need a vector of strings that start with E10... it would look something like this: [1] "E102" "E109" "E108" "E103" "E104" "E105" "E101" "E106" "E107" Thanks Ana
Jeff Newmiller
2020-May-15 17:13 UTC
[R] how to extract strings in any column and in any row that start with
Read about regular expressions... they are extremely useful. df1 <- tot %>% filter_all(any_vars(grepl( '^E10', .))) It is bad form not to put spaces around the <- assignment. On May 15, 2020 10:00:04 AM PDT, Ana Marija <sokovic.anamarija at gmail.com> wrote:>Hello, > >I have a data frame: > >> dim(tot) >[1] 502536 1093 > >How would I extract from it all strings that start with E10? > >I know how to extract all rows that contain with E10 >df0<-tot %>% filter_all(any_vars(. %in% c('E10'))) >> dim(df0) >[1] 5105 1093 > >but I just need a vector of strings that start with E10... >it would look something like this: > >[1] "E102" "E109" "E108" "E103" "E104" "E105" "E101" "E106" "E107" > >Thanks >Ana > >______________________________________________ >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.html >and provide commented, minimal, self-contained, reproducible code.-- Sent from my phone. Please excuse my brevity.
Ana Marija
2020-May-15 19:24 UTC
[R] how to extract strings in any column and in any row that start with
Hello,
this command was running for more than 2 hours
grep("E10",tot,value=T)
and no output
and this command
df1 <- tot %>% filter_all(any_vars(grepl( '^E10', .)))
gave me a subset (a data frame) of tot where ^E10
what I need is just a vector or all values in tot which start with E10.
Thanks
Ana
On Fri, May 15, 2020 at 12:13 PM Jeff Newmiller
<jdnewmil at dcn.davis.ca.us> wrote:>
> Read about regular expressions... they are extremely useful.
>
> df1 <- tot %>% filter_all(any_vars(grepl( '^E10', .)))
>
> It is bad form not to put spaces around the <- assignment.
>
>
> On May 15, 2020 10:00:04 AM PDT, Ana Marija <sokovic.anamarija at
gmail.com> wrote:
> >Hello,
> >
> >I have a data frame:
> >
> >> dim(tot)
> >[1] 502536 1093
> >
> >How would I extract from it all strings that start with E10?
> >
> >I know how to extract all rows that contain with E10
> >df0<-tot %>% filter_all(any_vars(. %in% c('E10')))
> >> dim(df0)
> >[1] 5105 1093
> >
> >but I just need a vector of strings that start with E10...
> >it would look something like this:
> >
> >[1] "E102" "E109" "E108" "E103"
"E104" "E105" "E101" "E106"
"E107"
> >
> >Thanks
> >Ana
> >
> >______________________________________________
> >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.html
> >and provide commented, minimal, self-contained, reproducible code.
>
> --
> Sent from my phone. Please excuse my brevity.
Abby Spurdle
2020-May-15 21:42 UTC
[R] how to extract strings in any column and in any row that start with
> How would I extract from it all strings that start with E10?Hi Ana, Here's a simple solution: x <- c ("P24601", "E101", "E102", "3.141593", "E101", "xE101", "e103", " E104 ") x [substring (x, 1, 3) == "E10"] You' will need to replace x with another *character vector*. (As touched on earlier, a data.frame may cause some problems). Here's some variations: unique (x [substring (x, 1, 3) == "E10"]) y <- toupper (x) y [substring (y, 1, 3) == "E10"] y <- trimws (x) y [substring (y, 1, 3) == "E10"]