r - Incorrect vector length after replacing certain values with NA -
i encountered strange problem:
say make following data frame
test<-as.data.frame(matrix(c(2,4,5,2,4,6),2,3,byrow=t)) # v1 v2 v3 # 1 2 4 5 # 2 2 4 6
then replace number 5 in column v3 row 1 na:
test$v3[test$v3==5]<-na # v1 v2 v3 # 1 2 4 na # 2 2 4 6
strangely, length of vector value 6 incorrect:
length(test$v3[test$v3==6]) # 2
how come output 2 instead of 1?
you can take apart expression see what's happening:
test$v3==6 # [1] na true
as can see, there na
value missing element. causes na
when subsetting test$v3
:
test$v3[test$v3==6] # [1] na 6
since vector of length 2, explains why code returns 2.
it sounds want count number of elements equal 6, ignoring missing values. with:
sum(test$v3 == 6, na.rm=true) # [1] 1
or
sum(!is.na(test$v3) & test$v3 == 6) # [1] 1
Comments
Post a Comment