Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R - how to check if all rows in a dataframe are NaN?

Tags:

dataframe

r

nan

How can check if all rows in a dataframe is empty or having NaN value?

My test data:

structure(list(site = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "HK6", class = "factor"), 
    code = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "HK6", class = "factor"), 
    date = structure(c(1492905600, 1492909200, 1492912800, 1492916400, 
    1492920000, 1492923600, 1492927200, 1492930800, 1492934400, 
    1492938000, 1492941600, 1492945200, 1492948800, 1492952400, 
    1492956000, 1492959600, 1492963200, 1492966800, 1492970400, 
    1492974000, 1492977600, 1492981200, 1492984800, 1492988400, 
    1492992000, 1492995600, 1492999200, 1493002800, 1493006400, 
    1493010000, 1493013600, 1493017200, 1493020800, 1493024400, 
    1493028000, 1493031600, 1493035200, 1493038800, 1493042400, 
    1493046000, 1493049600, 1493053200, 1493056800, 1493060400, 
    1493064000, 1493067600, 1493071200, 1493074800, 1493078400, 
    1493082000, 1493085600, 1493089200, 1493092800, 1493096400, 
    1493100000, 1493103600, 1493107200, 1493110800, 1493114400, 
    1493118000, 1493121600, 1493125200, 1493128800, 1493132400, 
    1493136000, 1493139600, 1493143200, 1493146800, 1493150400, 
    1493154000, 1493157600, 1493161200, 1493164800, 1493168400, 
    1493172000, 1493175600, 1493179200, 1493182800, 1493186400, 
    1493190000, 1493193600, 1493197200, 1493200800, 1493204400, 
    1493208000, 1493211600, 1493215200, 1493218800, 1493222400, 
    1493226000, 1493229600, 1493233200, 1493236800, 1493240400, 
    1493244000, 1493247600, 1493251200, 1493254800, 1493258400, 
    1493262000, 1493265600, 1493269200, 1493272800, 1493276400, 
    1493280000, 1493283600, 1493287200, 1493290800, 1493294400, 
    1493298000, 1493301600, 1493305200, 1493308800, 1493312400, 
    1493316000, 1493319600, 1493323200, 1493326800, 1493330400, 
    1493334000, 1493337600, 1493341200, 1493344800, 1493348400, 
    1493352000, 1493355600, 1493359200, 1493362800, 1493366400, 
    1493370000, 1493373600, 1493377200, 1493380800, 1493384400, 
    1493388000, 1493391600, 1493395200, 1493398800, 1493402400, 
    1493406000, 1493409600, 1493413200, 1493416800, 1493420400
    ), class = c("POSIXct", "POSIXt"), tzone = "GMT"), PM25 = c(NaN, 
    NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, 
    NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, 
    NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, 
    NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, 
    NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, 
    NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, 
    NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, 
    NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, 
    NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, 
    NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, 
    NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, 
    NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN)), row.names = c(NA, 
-144L), class = c("grouped_df", "tbl_df", "tbl", "data.frame"
), .Names = c("site", "code", "date", "PM25"), vars = list(site, 
    code), drop = TRUE, indices = list(0:143), group_sizes = 144L, biggest_group_size = 144L, labels = structure(list(
    site = structure(1L, .Label = "HK6", class = "factor"), code = structure(1L, .Label = "HK6", class = "factor")), row.names = c(NA, 
-1L), class = "data.frame", vars = list(site, code), drop = TRUE, .Names = c("site", 
"code")))

All rows at column PM25 are NaN, how can check if they are all NaN, then do something?

The column for PM25 is dynamic - sometimes it is PM10 or can be anything else.

Any ideas?

like image 836
Run Avatar asked Dec 10 '22 10:12

Run


1 Answers

Sample data

my.df <- data.frame(a=c(1, NA, 3), b=c(5, NA, NaN))
my.df
#    a   b
# 1  1   5
# 2 NA  NA
# 3  3 NaN

Identifying the rows having NA or NaN in all the columns.

ind <- rowSums(is.na(my.df)) == ncol(my.df)

Sample data

my.df <- data.frame(a=c(1, NA, 3), b=c(NA, NA, NaN))
my.df
#    a   b
# 1  1  NA
# 2 NA  NA
# 3  3 NaN

Identifying the columns having NA or NaN in all the rows.

ind <- colSums(is.na(my.df)) == nrow(my.df)
ind
#     a     b 
# FALSE  TRUE 

# to get the column names
names(my.df)[ind]

Specific Column only(as per OPs request):

sum(is.na(my.df[,'b'])) == nrow(my.df)

Thanks to Roland!

# alternate or best option
 all(is.na(my.df[,'b']))
like image 161
Prradep Avatar answered Dec 28 '22 23:12

Prradep