dataframe - R data frame subsetting concepts -
i have data frame called data. have column called colb na values , column called colc integer values , no nas.
when use subset syntax false results (all nas):
nrow(data[data$colb == "xxxx",])
but when run same syntax using function no results, expect , want.
data[which(data$colb== "xxxx"),]
also, when run following:
{data[data$colc == 185,]}
i correct result/output 1 matching row.
why this? having na in data? , subsetting column reference (instead of using subset function) without using not recommended?
na
a logical constant of length 1 contains missing value indicator.
and e.g. which( c( na,na,na) )
returns 0 length integer vector none of comparisons can considered true
, same if tried which( c(false,false,false) )
. therefore trying subset data.frame nothing, returning nothing.
if need comparisons using data na
values, use is.na()
, function, e.g. consider:
x <- c( na , is.na(na) , na ) which( x ) #[1] 2
Comments
Post a Comment