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

I want to retrieve only numeric values

vector<-c("0.78953744969927742", "0.46557689748480685", "0.19740881059705201", 
  "9.7073839462985714E-2", "4.9051709747422199E-2", "0.1167420589551126", 
  "0.12679434401288708", "0.51370748568563795", "0.1925345466801483", 
  "0.48287163643195624", "4.211984449707315E-2", "blablablab", 
  "0.10553766233766231", "7.8187250996015922E-2", "0.20718689788053954", 
  "1.6450511945392491E-2", "0.51752961082910309", "0.10978571428571428", 
  "0.42610062893081763", "0.52208333333333334", "0.27569868995633184", 
  "7.7189939288811793E-2", "0.53982300884955747", "38.25% (blablabla) blablablablablablablablablablablabla","0.22324159021406728")

I have to transform all observations into numerical values. Those consisting only of words in NA. If there are words after an observation starting with a number; retrieve only the numbers. If there are percentages after the number, eliminate these percentages and keep only the number

>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

In R, you can use the as.numeric() function to convert the elements of the vector to numerical values. However, this function will return NA for elements that cannot be converted to a number. To filter out elements that consist only of words, you can use the is.na() function to identify the NA values and remove them from the vector.

Here’s some code that demonstrates how to do this:

# convert elements of vector to numeric values
vector <- as.numeric(vector)

# remove elements that are NA
vector <- vector[!is.na(vector)]

# remove the trailing % and (blablabla) from the elements
vector <- gsub("% \\(.*\\)","",vector)

# remove the trailing % from the elements
vector <- gsub("%","",vector)
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