Ana Marija
2020-Apr-10 16:15 UTC
[R] how to get all strings in a data frame that start with a particular string
Hello, Hello, I have a data frame (tot) with about 2000 columns. How can I extract from it all strings that start with E14? I tried this: e14 <- sapply(tot, function(x) grepl("^E14", x)) but this returns me just TRUE and FALSE vector, how do I get actual strings that start with E14? Thanks Ana
Michael Dewey
2020-Apr-10 16:43 UTC
[R] how to get all strings in a data frame that start with a particular string
Dear Ana Would it not be possible to use grep instead of grepl and get the values using the value = TRUE parameter? Michael On 10/04/2020 17:15, Ana Marija wrote:> Hello, > > Hello, > > I have a data frame (tot) with about 2000 columns. How can I extract > from it all strings that start with E14? > > I tried this: > e14 <- sapply(tot, function(x) grepl("^E14", x)) > > but this returns me just TRUE and FALSE vector, how do I get actual > strings that start with E14? > > 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. >-- Michael http://www.dewey.myzen.co.uk/home.html
Rasmus Liland
2020-Apr-10 17:00 UTC
[R] how to get all strings in a data frame that start with a particular string
On 2020-04-10 11:15 -0500, Ana Marija wrote:> I have a data frame (tot) with about > 2000 columns. How can I extract from > it all strings that start with E14? > > I tried this: > e14 <- sapply(tot, function(x) grepl("^E14", x)) > > but this returns me just TRUE and > FALSE vector, how do I get actual > strings that start with E14?Dear Ana, perhaps you thought of something along the lines of this: ncol <- 2000 nrow <- 3 line <- c("a", "b", "some text E14 bla bla some more text", "d", "E14 ... hey this starts and also ends with E14", "E14 something-something", "another string") tot <- as.data.frame(matrix(rep(line, times=ncol*nrow), ncol=ncol, byrow=T)) # Now, tot is a df with some cells # containing replicates of line, some # cells there are now starting with E14 # ... so we need to convert it to a # character matrix to be able to find # the indecies of the cells starting # with E14: tot <- as.matrix(tot) idx <- grepl("^E14", tot) tot[idx] Best, Rasmus
Ana Marija
2020-Apr-10 18:14 UTC
[R] how to get all strings in a data frame that start with a particular string
Thank you so much! On Fri, Apr 10, 2020 at 12:00 PM Rasmus Liland <jensrasmus at gmail.com> wrote:> > On 2020-04-10 11:15 -0500, Ana Marija wrote: > > I have a data frame (tot) with about > > 2000 columns. How can I extract from > > it all strings that start with E14? > > > > I tried this: > > e14 <- sapply(tot, function(x) grepl("^E14", x)) > > > > but this returns me just TRUE and > > FALSE vector, how do I get actual > > strings that start with E14? > > Dear Ana, > > perhaps you thought of something along > the lines of this: > > ncol <- 2000 > nrow <- 3 > line <- > c("a", "b", > "some text E14 bla bla some more text", > "d", > "E14 ... hey this starts and also ends with E14", > "E14 something-something", > "another string") > tot <- > as.data.frame(matrix(rep(line, > times=ncol*nrow), > ncol=ncol, > byrow=T)) > > # Now, tot is a df with some cells > # containing replicates of line, some > # cells there are now starting with E14 > # ... so we need to convert it to a > # character matrix to be able to find > # the indecies of the cells starting > # with E14: > > tot <- as.matrix(tot) > idx <- grepl("^E14", tot) > tot[idx] > > Best, > Rasmus