----- Original Message ----
From: Shang Liu <[EMAIL PROTECTED]>
Subject: [R] how to replace NA values in a list

I have a matrix named "spec" (see below), it is a 6x3 matrix, and each element 
of spec is a list. For example, spec[1,"wavenumber"] is a list, and it contains 
1876 numeric numbers and NAs. I want to replace the NAs to zero, but don't know 
how to change it, the difficulty may be all the elements are of the class list, 
so it is hard to change. 

Thank you for your help! 

matrix spec:

     wavenumber   prescan      postscan    
H001 Numeric,1876 Numeric,1876 Numeric,1876
H002 Numeric,1876 Numeric,1876 Numeric,1876
H003 Numeric,1876 Numeric,1876 Numeric,1876
H004 Numeric,1876 Numeric,1876 Numeric,1876
H005 Numeric,1876 Numeric,1876 Numeric,1876
H006 Numeric,1876 Numeric,1876 Numeric,1876
    [[alternative HTML version deleted]]

===


Hi Shang,

spec[1,"wavenumber"] is a list, but spec[[1,"wavenumber"]] is a vector 
(Numeric,1876).

Something like

spec[[1,"wavenumber"]] <- 
replace(spec[[1,"wavenumber"]],is.na(spec[[1,"wavenumber"]]),0)

or

spec[,"wavenumber"] <- lapply(spec[,"wavenumber"],function(x) 
replace(x,is.na(x),0))

or

spec[] <- lapply(spec,function(x) replace(x,is.na(x),0))

could work, depending on how many of the matrix's elements you want to replace.

Satoshi

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to