Data frame challenge [🇷 for BE/BA]

posted by ElMaestro  – Denmark, 2016-12-18 12:47 (3050 d 01:58 ago) – Posting: # 16854
Views: 4,696

Hi Mittyri,

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)



My eyes! My eyes! It hurts. :lol3::lol3:

Pass or fail!
ElMaestro

Complete thread:

UA Flag
Activity
 Admin contact
23,424 posts in 4,927 threads, 1,668 registered users;
24 visitors (0 registered, 24 guests [including 10 identified bots]).
Forum time: 15:46 CEST (Europe/Vienna)

It is a well-known experience that the only truly enjoyable and
profitable way of studying mathematics is the method of
“filling in details” by one’s own efforts.    Cornelius Lanczos

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