简体   繁体   中英

How can I print a matrix in R with NA values hidden?

I have a matrix that contains some NA elements (eg mat below), and I want to make a new function that prints it with the NA values hidden (ie as fun below). How can I achieve this?

mat <- cbind(c(1,2,NA,NA),c(3,3,3,NA),c(NA,4,4,4),c(NA,NA,5,5))
print(mat)

     [,1] [,2] [,3] [,4]
[1,]    1    3   NA   NA
[2,]    2    3    4   NA
[3,]   NA    3    4    5
[4,]   NA   NA    4    5

fun(mat)

     [,1] [,2] [,3] [,4]
[1,]    1    3          
[2,]    2    3    4     
[3,]         3    4    5
[4,]              4    5

We can use na.print in print

print(mat, na.print = "")
#      [,1] [,2] [,3] [,4]
#[1,]    1    3          
#[2,]    2    3    4     
#[3,]         3    4    5
#[4,]              4    5

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM