I have imported a csv sheet (319 columns x 45 rows). The dataset is highly confidential so I can't post any part of it. The class is a data.frame.
There are a large number of "Null" values spread across all of the columns. The senior manager wants all the "Null" values converted to -9. So I tried the following code...
df[df == "Null"] <- -9
Absolutely nothing changed in the dataset. I checked the names of the dataset and that's fine. So I created a dummy dataset to check I had the correct code. The code worked fine on the dummy dataset. Any ideas what could be wrong? Sorry I can't post the data.
It is great that you solved your challenge, but I still think that in order to get a full learning experience, you should make sure, that you understand the difference between NULL and "NULL". The first represents the null object in R and the latter is a string/character. This is what I was hinting at in my first post:
What you are doing is not looking for NULL values, but looking for specific strings, when you do
v[v == "NULL"]
Furthermore, you have the remaining values "1" and "9" as strings, so what you want to do is the following:
# Load libraries
library("tidyverse")
# Define dummy tibble
d = tibble(v = c("NULL", "1", "9", "NULL"))
# View dummy tibble
# note the <chr>, which tells you that v is a character
d
# A tibble: 4 x 1
v
<chr>
1 NULL
2 1
3 9
4 NULL
# Replace "NULL" with NA and convert to numeric
# note the <dbl> which tells you that v is now a numeric
# (Skipping `numeric` vs. `double` vs. `integer` for now)
d %>% mutate(v = ifelse(v == "NULL", NA, v) %>% as.numeric)
# A tibble: 4 x 1
v
<dbl>
1 NA
2 1.
3 9.
4 NA
Hope it helps and I really would highly recommend, that you go through R for Data Science to get a better understanding of the above concepts in R
Hi Leon, I'm really impressed with the time you've taken to put this together including the content. I am currently working my work through Hadley's R for data science. I must admit this isn't something I've given any thought to previously because it's the first time I've come across nulls in a dataset. I suspect that's why it was given to me to do. We normally create and analyse our own data from the SQL server. However this dataset was sent from another organisation. R is a constant learning curve! I will practise with the code you sent - Many thanks