Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Tibble select world and add column

I have a list of word and phrases and I would like create a DB that select that word and add in a column, not delete but add. This is how I want do, how can I do?

library(dplyr)
library(tidyr)
|      Heading 1      | 
|---------------------|
|   Hello world       |
|            Say world|
|        Say something|
tibble(
  "Hello world", 
  "Say world", 
  "Say something"
)


list_of_words <- tolower(c("world","something"))

|      Heading 1      |      Heading 2      | 
|---------------------|---------------------|
|   Hello world       |   world       |
|            Say world|         world|
|        Say something|       something| 

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You could use regmatches like this:

DB <- data.frame(heading_1 = c("Hello world", "Say world", "Say something"))
DB
#>       heading_1
#> 1   Hello world
#> 2     Say world
#> 3 Say something
list_of_words <- tolower(c("world","something"))
words <- paste(list_of_words, collapse="|")
DB$heading_2 <- unlist(regmatches(DB$heading_1, gregexpr(words, DB$heading_1)))
DB
#>       heading_1 heading_2
#> 1   Hello world     world
#> 2     Say world     world
#> 3 Say something something

Created on 2022-07-21 by the reprex package (v2.0.1)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading