0

I checked my matrix for NA, Inf and -Inf. There are no occurrences. What is happening?

head(peaks.new)
           X99     X111     X127      X191      X196     X273      X347      X357      X372     X430      X542      X601      X676      X688     X730
[1,] 0.5804457 1.144064 1.060764 0.3957671 0.4605744 1.395375 0.3997679 0.4058198 0.6135423 1.024926 0.3185467 0.4280903 0.4200074 0.4150762 1.026295
[2,] 0.6738807 1.228152 1.199513 0.4588390 0.5470579 1.383542 0.4491294 0.4576340 0.7606879 1.188590 0.3476853 0.4771873 0.5021612 0.4848465 1.165312
[3,] 0.6315261 1.235551 1.234193 0.4415364 0.5230350 1.514145 0.4512593 0.4589265 0.7238682 1.180188 0.3389439 0.4703305 0.4733625 0.4622625 1.175494
[4,] 0.6482746 1.240872 1.270660 0.5258102 0.5381634 1.430304 0.4888256 0.4987946 0.8740429 1.189461 0.3790521 0.5155980 0.4893849 0.4897720 1.090612
[5,] 0.6110295 1.215108 1.162420 0.4374122 0.4988751 1.435029 0.4320153 0.4360902 0.7144469 1.148086 0.3276657 0.4432349 0.4520939 0.4402073 1.129200
[6,] 0.7070189 1.344910 1.254874 0.4893428 0.5730396 1.708531 0.4627560 0.4797072 0.7752893 1.229642 0.3612133 0.4825547 0.5037152 0.4896373 1.267938

pca.peaks <- prcomp(t(peaks.new), scale=T, retx=T, center=T)
Error in svd(x, nu = 0, nv = k) : infinite or missing values in 'x'


which(peaks.new==0)
integer(0)
> which(peaks.new==Inf)
integer(0)
> which(peaks.new==-Inf)
integer(0)
> which(peaks.new==NA)
integer(0)
decapicone
  • 23
  • 6
  • Instead, you want to use `which(is.na(peaks.new))`. See https://stackoverflow.com/questions/53379501/in-r-does-na-na – slamballais May 17 '21 at 20:38

1 Answers1

0

NAs are tricky. Consider this:

> a = c(1, NA, 2)
> which(a == NA)
integer(0)

> a == NA
[1] NA NA NA

Equality checks with NA will result in NA. The proper way of checking for NAs is with the is.na() function:

> is.na(a)
[1] FALSE  TRUE FALSE

There's also is.infinite() for the case of Inf, though in that case direct comparison works (e.g. (1/0) == Inf yields TRUE).

Álvaro
  • 564
  • 5
  • 13
  • Gotcha. Thank you! Turns out there are NaN's in my data. Now to figure out how they got there... lol – decapicone May 17 '21 at 20:58
  • Well, the NAs could either be missing since the beginnig (maybe check for NAs in the original dataset) or introduced as a side effect of some transformation (e.g. it's log transformed and there were negative numbers, you accidentally divided by 0...). Also, if it solved your question, please consider accepting the answer to let people know the matter is closed ^^ – Álvaro May 18 '21 at 09:34
  • thank you Alvaro. I accepted your answer! Yes the data was log transformed and I figured out the source of NaN's – decapicone May 20 '21 at 19:19