Data frame challenge [🇷 for BE/BA]

posted by mittyri – Russia, 2016-12-18 02:41 (2974 d 13:54 ago) – Posting: # 16851
Views: 4,597

Hi ElMaestro,

I'd like
A=read.table("SomeFile.csv", header=T, stringsAsFactors = F, sep=",")
apply(A, 2, function(x){
       as.numeric(replace(x,
         !grepl("[-]?[0-9]+[.]?[0-9]*|[-]?[0-9]+[L]?|[-]?[0-9]+[.]?[0-9]*[eE][0-9]+",x), 0))})


But more readable would be
A=read.table("SomeFile.csv", header=T, stringsAsFactors = F, sep=",")
checkandreplace <- function(x){
  # which values are looking not as a numeric?
  isnotnumeric <- !grepl("[-]?[0-9]+[.]?[0-9]*|[-]?[0-9]+[L]?|[-]?[0-9]+[.]?[0-9]*[eE][0-9]+",x)
  # we need to replace them with 0
  replaced0 <- replace(x, isnotnumeric, 0)
  # and to convert it all to numeric
  return(as.numeric(replaced0))
}
apply(A, 2, checkandreplace)


Enjoy!

Kind regards,
Mittyri

Complete thread:

UA Flag
Activity
 Admin contact
23,376 posts in 4,912 threads, 1,667 registered users;
105 visitors (0 registered, 105 guests [including 11 identified bots]).
Forum time: 16:35 CET (Europe/Vienna)

The great tragedy of Science – the slaying
of a beautiful hypothesis by an ugly fact.    Thomas Henry Huxley

The Bioequivalence and Bioavailability Forum is hosted by
BEBAC Ing. Helmut Schütz
HTML5