Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do not print NA when printing data frame

For data frames there is no na.print option. Is there any workaround to suppress display of NAs?

Sample data frame:

df <- data.frame(
       x=c("a","b","c","d"),
       a=c(1,1,1,1),
       b=c(1,1,1,NA),
       c=c(1,1,NA,NA),
       d=c(1,NA,NA,NA))      
df

Results in:

  x a  b  c  d
1 a 1  1  1  1
2 b 1  1  1 NA
3 c 1  1 NA NA
4 d 1 NA NA NA

But I would like to show:

  x a  b  c  d
1 a 1  1  1  1
2 b 1  1  1
3 c 1  1
4 d 1
like image 882
Tomas Greif Avatar asked Oct 22 '13 11:10

Tomas Greif


People also ask

How to exclude NA values from calculation in R?

First, if we want to exclude missing values from mathematical operations use the na. rm = TRUE argument. If you do not exclude these values most functions will return an NA . We may also desire to subset our data to obtain complete observations, those observations (rows) in our data that contain no missing data.

How to check for missing values NA in R?

To identify missing values use is.na() which returns a logical vector with TRUE in the element locations that contain missing values represented by NA . is.na() will work on vectors, lists, matrices, and data frames.

How to define missing values in R?

In R, missing values are represented by the symbol NA (not available). Impossible values (e.g., dividing by zero) are represented by the symbol NaN (not a number). Unlike SAS, R uses the same symbol for character and numeric data.

What function is used to test the missing observation in data frame?

Checking for missing values using isnull() and notnull() In order to check missing values in Pandas DataFrame, we use a function isnull() and notnull(). Both function help in checking whether a value is NaN or not.


1 Answers

You can replace missing values by "" (this technique is used in print.dist S3 method)

cf <- format(dat) ## use format to set other options like digits, justify , ...
cf[is.na(dat)] <- ""
 cf
  x a  b  c  d
1 a 1  1  1  1
2 b 1  1  1   
3 c 1  1      
4 d 1    
like image 88
agstudy Avatar answered Sep 30 '22 19:09

agstudy