Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
214 views
in Technique[技术] by (71.8m points)

extract - Extracting specific data from text column in R

I have a data set of medicine names in a column. I am trying to extract the name ,strength and unit of each medicine from this data. The term MG and ML are the qualifiers of strength in the setup. For example, let us consider the following given data set for the names of the medicines.

 Medicine name
----------------------
 FALCAN 150 MG tab
 AUGMENTIN 500MG tab
 PRE-13 0.5 ML PFS inj
 NS.9%w/v 250 ML, Glass Bottle

I want to extract the following information columns from this data set,

Name     | Strength |Unit
---------| ---------|------
FALCAN   | 150      |MG
AUGMENTIN| 500      |MG
PRE-13   | 0.5      |ML
NS.9%w/v | 250      |ML

I have tried grepl etc command and could not find a good solution. I have around >12000 data to identify. Data does not follow a fixed pattern, and at few places MG and strength does not have a space in between such as 300MG. ,

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If the input L is as given reproducibly in the Note at the end then use sub to replace MG or ML and everything after with a space followed by MG or ML and then read it using read.table:

s <- sub("(M[GL]).*", " \1", L)
read.table(text = s, as.is = TRUE, skip = 1, col.names = c("Name", "Strength", "Unit"))

giving:

       Name Strength Unit
1    FALCAN    150.0   MG
2 AUGMENTIN    500.0   MG
3    PRE-13      0.5   ML
4  NS.9%w/v    250.0   ML

Note: The input L used is:

L <- c("Medicine name", " FALCAN 150 MG tab", " AUGMENTIN 500MG tab", 
" PRE-13 0.5 ML PFS inj", " NS.9%w/v 250 ML, Glass Bottle")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...