+ Reply to Thread
Results 1 to 3 of 3

Thread: Empty cell when importing csv file

  1. #1
    Points: 2,816, Level: 32
    Level completed: 44%, Points required for next Level: 84

    Posts
    35
    Thanks
    0
    Thanked 1 Time in 1 Post

    Empty cell when importing csv file




    Dear-

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

    For example,

    a <- read.csv(file="c:\\order4.csv", header=TRUE, sep=",");
    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?

    Thanks in advance.

    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.

  2. #2
    Probably A Mammal
    Points: 32,065, Level: 100
    Level completed: 0%, Points required for next Level: 0
    bryangoodrich's Avatar
    Location
    Sacramento, California, United States
    Posts
    2,567
    Thanks
    398
    Thanked 618 Times in 551 Posts

    Re: Empty cell when importing csv file

    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 <- read.csv(...)
    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)
    You should definitely use jQuery. It's really great and does all things.

  3. #3
    Points: 2,816, Level: 32
    Level completed: 44%, Points required for next Level: 84

    Posts
    35
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Empty cell when importing csv file


    Appreciated!

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

+ Reply to Thread

           




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts






Advertise on Talk Stats