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, to be honest, I never created a matrix of lists before, but hopefully this code will help you? set.seed(12345) my.pool <- c(NA, 0:10) n <- 25 alist <- list(sample(x=my.pool, size=n, replace=TRUE)) alist mymatrix <- matrix(rep(alist, 6*3), nrow=6) mymatrix2 <- lapply(X=mymatrix, FUN=function(x) ifelse(is.na(x),0,x)) mymatrix2 Best, Roland Shang Liu wrote:> 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]] > > ______________________________________________ > R-help at 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. >
----- 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