Quantcast
Viewing all articles
Browse latest Browse all 206503

How to filter and copy a value in R dataframe?

I have a dataframe like as shown below

Test <- c('Sodium','Heamo Index.','Lipae Index','ictoric index','Chloride','Blood pressure','Test Index')
value <- c(12,'No haemo',NA,'No ict',21,32,NA)
TextualResults <- c('low value',NA,'NO LIPA',NA,'HIGH','low','NO TEST')

df_test <- data.frame(Test,value,TextualResults)

The input looks like as shown below

Image may be NSFW.
Clik here to view.
enter image description here

I was trying something like this

library(tidyverse)

df_test %>%  
  filter(stringr::str_detect(type, 'index|Index|INDEX'))

But not sure how to copy the value based on NA check and make the search case-insensitive in a elegant way

I expect my output to be like as shown below

Image may be NSFW.
Clik here to view.
enter image description here

As you can see that under Test column, whenever we find values which contain index (case insensitive), we have to make sure that value column is not NA for those rows.

Data by default might have value for index rows under value column but it could be NA as well.

So, when it's NA we pick whatever value is present under TextualResults column and put it back in value column for rows containing index term. Basically value column will never be NA for rows containing index term


Viewing all articles
Browse latest Browse all 206503

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>