[R] how to replace NA values in a list
Satoshi Takahama
s.takahama at yahoo.com
Sat Aug 2 05:44:58 CEST 2008
----- Original Message ----
From: Shang Liu <liushang at ucsd.edu>
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
More information about the R-help
mailing list