r - Delete Last Part of a String Starting with a Numeric Value -


i have dataframe of drug names. there multiple doses each type of drug. instance, have:

 x <- data.frame(c("drugx 10 mg", "drugx 20 mg", "drugx 30mg", "drugx 2% cream", "drugx 10% gel", "drugy 20 mg", "drugy 30 mg"))   x[,1] <- as.character(x[,1]) 

i delete after given numeric value. new dataframe looks this:

 xnew <- data.frame(c("drugx", "drugx", "drugx", "drugx", "drugx", "drugy", "drug y")) 

at point take 'uniques'

 xnew2 <- unique(xnew) 

so final product be

 xnew2 <- c("drugx", "drug y") 

thanks in advance!

you can try sub

v1 <- sub('\\s*\\d+.*$', '', x[,1]) v1 #[1] "drugx" "drugx" "drugx" "drugx" "drugx" "drugy" "drugy"  unique(v1) #[1] "drugx" "drugy" 

Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -