R - svd() function - infinite or missing values in 'x'
You have columns composed of all zeroes. Using scale
on a column of all zeroes returns a column composed of NaN
. To solve this, remove columns where you have all zeroes (svd
will not reveal anything new about them), or replace NaN
columns with zero after using scale
.
Reproducible example:
mat <- matrix(c(1,2,3,0,0,0,2,4,6,5,12,13),nrow = 3)
# [,1] [,2] [,3] [,4]
# [1,] 1 0 2 5
# [2,] 2 0 4 12
# [3,] 3 0 6 13
scale(mat)
# [,1] [,2] [,3] [,4]
# [1,] -1 NaN -1 -1.1470787
# [2,] 0 NaN 0 0.4588315
# [3,] 1 NaN 1 0.6882472
# attr(,"scaled:center")
# [1] 2 0 4 10
# attr(,"scaled:scale")
# [1] 1.000000 0.000000 2.000000 4.358899
svd(mat) #fine
svd(scale(mat)) # not fine
Error in svd(x, nu = 0, nv = k) : infinite or missing values in 'x'. There are no NA or Inf values in matrix
NA
s 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
).
Related Topics
Count Unique Combinations of Values
Cannot Read File with "#" and Space Using Read.Table or Read.CSV in R
Filling Under the a Curve with Ggplot Graphs
Print Tibble with Column Breaks as in V1.3.0
Store Arrangegrob to Object, Does Not Create Printable Object
"Unpacking" a Factor List from a Data.Frame
Combining Geom_Point and Geom_Line with Position_Jitterdodge for Two Grouping Factors
Pass R Variable to Rodbc's SQLquery with Multiple Entries
Testing a Function That Uses Enquo() for a Null Parameter
How to Run a R Language(.R) File Using Batch File
Loess Fit and Resulting Equation
Shiny Rcharts Multiple Chart Output
Combine Multiple PDF Plots into One File