Day 17 – little helper to_na

We at STATWORX work a lot with R and we often use the same little helper functions within our projects. These functions ease our daily work life by reducing repetitive code parts or by creating overviews of our projects. At first, there was no plan to make a package, but soon I realised, that it will be much easier to share and improve those functions, if they are within a package. Up till the 24th December I will present one function each day from helfRlein. So, on the 17th day of Christmas my true love gave to me…

What can it do?

This little helper is just a convenience function. Some times during your data preparation, you have a vector with infinite values like Inf or -Inf or even NaN values. Thos kind of value can (they do not have to!) mess up your evaluation and models. But most functions do have a tendency to handle missing values. So, this little helper removes such values and replaces them with NA.

How to use it?

A small exampe to give you the idea:

1
2
3
4
5
6
test <- list(a = c("a", "b", NA),
 b = c(NaN, 1,2, -Inf),
 c = c(TRUE, FALSE, NaN, Inf))

lapply(test, to_na)

1
2
3
4
5
6
7
8
9
$a
[1] "a" "b" NA 

$b
[1] NA 1 2 NA

$c
[1] TRUE FALSE NA

A little advice along the way! Since there are different types of NA depending on the other values within a vector. You might want to check the format if you do to_na on groups or subsets.

1
2
3
test <- list(NA, c(NA, "a"), c(NA, 2.3), c(NA, 1L))
str(test)

1
2
3
4
5
6
List of 4
 $ : logi NA
 $ : chr [1:2] NA "a"
 $ : num [1:2] NA 2.3
 $ : int [1:2] NA 1

Overview

To see all the other functions you can either check out our GitHub or you can read about them here.

Have a merry advent season!

Über den Autor

Jakob Gepp

Numbers were always my passion and as a data scientist and statistician at STATWORX I can fullfill my nerdy needs. Also I am responsable for our blog. So if you have any questions or suggestions, just send me an email!


STATWORXis a consulting company for data science, statistics, machine learning and artificial intelligence located in Frankfurt, Zurich and Vienna. Sign up for our NEWSLETTER and receive reads and treats from the world of data science and AI. 

**