# Empty cell when importing csv file

#### jk90029

##### Member
Dear-

We are going to import a csv format file to R system.

For example,

typeof(a);
***(a);

are used to import. However the new file a in R is list, instead of matrix which is easily used for us.

1) What is the *** command to convert from list to matrix?

2) The a file has lots of empty cell (NA ot Null). Can we put zero ("0") to all empty cell in R?

PS) Possible candidate answer is sapply(a, is.na) and read.csv(***, na.strings("", "NA")) or something like that.

PS) For further illustration, I can make realistic example to explain situation.

#### bryangoodrich

##### Probably A Mammal
1. Don't need to indicate sep="," when you're using read.csv. The point of read.csv is a convenience version of read.table that presets some parameters, like sep = ","
2. Your data imports as a data.frame. When you do typeof it comes back as list because data frames are, behind the scenes, a type of list: a list of column vectors of equal length.
3. If you want to post-process na values into 0s that's a simple operation

Code:
x$mycolumn[is.na(x$mycolumn)] <- 0
But if you want to blanket this across the entire data frame (list), you can use lapply in a tricky way

Code:
replace_na <- function(x, value=0)
{
x[is.na(x)] <- value
x
}
x[] <- lapply(x, replace_na)
Here replace_na takes in a vector and replaces it with a value (default to 0). With data frames, since they're column vectors in a list, lapply will loop across all its columns. We're replacing the contents of x (x[]) with the resultant vectors.

Caveat: this doesn't work with factors, so I'd recommend including the stringsAsFactor = FALSE parameter in your read.csv statement to avoid them.

Alternatively, you can select which columns to replace

Code:
x[, c(1, 3, 5)] <- lapply(x[, c(1, 3, 5)], replace_na)

#### jk90029

##### Member
Appreciated!

In particular, the CAVEAT above helped me a lot.
This is best answer among several posts to many places, for sure.