[R] Text search
Sarah Goslee
@@r@h@go@|ee @end|ng |rom gm@||@com
Sun Jan 26 00:37:48 CET 2020
Hi,
> dat$new[grep("Tall", dat$char)] <- "Tall"
> dat$new[grep("Short", dat$char)] <- "Short"
> dat
obs Year char new
1 1 2001 Tall156 Tall
2 2 2002 12565Tall Tall
3 3 2003 all54
4 4 2004 Short Short
5 5 2005 54all
6 6 2006 7Short12 Short
Assuming that no entries contain both Tall and Short.
Sarah
On Sat, Jan 25, 2020 at 6:23 PM Ashta <sewashm using gmail.com> wrote:
>
> Hi all,
> From one of the columns of the data frame I want to search and
> extract a text that contains Tall or Short and create new column
> that should contain these texts in a corresponding row.
> My example data and the desired output are shown below
>
> dat<-read.table(text="obs Year char
> 1 2001 Tall156
> 2 2002 12565Tall
> 3 2003 all54
> 4 2004 Short
> 5 2005 54all
> 6 2006 7Short12 ",header=TRUE,stringsAsFactors=F)
> dat$new <- " "
>
> Desired out put
> obs Year char new
> 1 2001 Tall156 Tall
> 2 2002 12565Tall Tall
> 3 2003 all54
> 5 2004 Short Short
> 6 2005 Shall54
> 7 2006 7Short12 Short
>
> How do I get my desired output?
> Thank you.
>
> ______________________________________________
> 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.
--
Sarah Goslee (she/her)
http://www.numberwright.com
More information about the R-help
mailing list