I have a dataframe, let's say it's:
name = c("Joe", "Tim", "Sally")
code = c(43, NA, 19)
address = c("123 street Rd.", "911 Emergency Ln.", "NULL")
date = as.Date(c("2021-11-02", "", "2021-03-29"))
data <- data.frame(name, code, address, date)
and I want to clean the dataset by changing numeric columns to NaN (unless this makes it difficult to graph), character columns to "not provided", and date values to NA or whatever is suggested.
I have been trying to use an if statement inside a for loop like:
columns <- colnames(data)
for (column in columns){
if (is.numeric(data[column]){
data[column][is.na(data[column])] = 0}
elseif (is.numeric(data[column]){
data[column][is.character(data[column])] = "not provided"}
else data[column][is.date(data[column])] = "1900-1-1"
but this is not working the way I thought it should. I am looking for something that looks similar to:
| name | code | address | date |
|---|---|---|---|
| Joe | 43 | 123 street Rd. | 2021-11-02 |
| Tim | Nan | not provided | 1901-1-1 |
| Sally | 19 | 911 Emergency Ln. | 2021-03-29 |
I am learning about handling large datasets (>1 million rows) using tidyverse, but I am struggling on this type of problem. How can I clean data in a way that results in what I'm looking for?