nouse
nouse

Reputation: 3461

Using only numeric values in an object

testi have a table containing numeric values, including zeros. I want to do a log-transformation on columns, yielding character strings "-Inf". A shapiro.test consequently results in "NA" for any column containing characters after the log transformation.

However, is it possible to extract just the numeric values from that column and perform a shapiro.test afterwards? Maybe i can replace the "-Inf" with nothing?

Thank you for your help!

test <- log(x)
test
 [1] -Inf -7.562929 -7.424757 -6.284397 -6.637388 -8.141037 -5.371136 -7.093784 -6.489694 -6.529703
[11] -7.282913 -7.075025 -7.711110 -9.352510 -6.580074 -6.530339 -5.549224 -7.347854 -4.088114 -Inf (rest omitted)

str(test)
num [1:60] -7.32 -7.56 -7.42 -6.28 -6.64 ...

shapiro.test(test)

    Shapiro-Wilk normality test

data:  test
W = NaN, p-value = NA

Upvotes: 0

Views: 1565

Answers (2)

calejero
calejero

Reputation: 424

You can extract a subset with all finite values using is.finite() function.

a = c(pi/0, 3,4,5,2,3)
a
[1] Inf  3  4  5  2  3
a = a[is.finite(a)]
a
[1] 3  4  5  2  3

Upvotes: 2

nicola
nicola

Reputation: 24480

In R Inf is a numeric value and not a string. It indicates the biggest possible double precision number. If you want to remove infinite values from a vector, you can try with the is.finite function, for instance:

    test[is.finite(test)] 

Of course this will just remove the infinite, but it doesn't guarantee that what you are doing makes sense.

Upvotes: 1

Related Questions