[R] convert character vector to decimal for numeric testing

2015-07-15 Thread Luigi Marongiu
Dear all,
I have a vector that comes from some calculations I am making and this
vectors turns out to be in character format rather than numerical. I
can convert it in numerical format but then the calculations are not
correct, as you can see in the following example. I was also expecting
that rounding a number such as 5.43 to a three digits one would return
5.430 but that did not happen. Any tips on how to apply the
calculation correctly?
Thank you
best regards
luigi

>>>
vec.ori <- c("5.43", "6.63", "-1.18593063116494e+36", "6.2", "5.61",
"4.96842801255869e+30", "5.59", "-Inf", "Inf", "5.49", "18.35",
"-3.11", "6.07", NA)

vec.num <- as.numeric(vec.ori)

vec.num <0

[1] FALSE FALSE  TRUE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE
TRUE FALSENA

vec.num >0

 [1]  TRUE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE
FALSE  TRUENA

for(i in 1:length(vec.num)) {
  cat("value at beginning: ", vec.num[i], "\n", sep="")
  if(vec.num[i] < 0) {
vec.num[i] <- "LO"
  } else if(vec.num[i] > 45) {
vec.num[i] <- "HI"
  } else if (is.na(vec.num[i])== TRUE) {
vec.num[i] <- "na"
  } else if (is.infinite(vec.num[i]) == TRUE) {
vec.num[i] <- "INF"
  } else {
vec.num[i] <- round(vec.num[i], 3)
  }
  cat("value at end: ", vec.num[i], "\n", sep="")
}

value at beginning: 5.43
value at end: 5.43
value at beginning: 6.63
value at end: 6.63
value at beginning: -1185930631164940020264024442864400022
value at end: LO   # REM: error!
value at beginning: 6.2
value at end: HI   # REM: error!
value at beginning: 5.61
value at end: HI   # REM: error!
value at beginning: 4968428012558689723622822000404
value at end: HI
value at beginning: 5.59
value at end: HI   # REM: error!
value at beginning: -Inf
value at end: LO   # REM: error!
value at beginning: Inf
value at end: HI   # REM: error!
value at beginning: 5.49
value at end: HI   # REM: error!
value at beginning: 18.35
Error in round(vec.num[i], 3) :
  non-numeric argument to mathematical function
# REM: cycle crashed

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] convert character vector to decimal for numeric testing

2015-07-15 Thread jim holtman
It does round to 3 digits, but since the last one is a zero, is only prints
5.43 and not 5.430.  Why do you want the last zero?  Is this going into a
report or something?  You can always use sprintf:

> x <-round(5.43, 3)
> x
[1] 5.43
> sprintf("%.3f", x)
[1] "5.430"
>

BTW you are converting your numeric vector back to character with
statements like:

 if(vec.num[i] < 0) {
vec.num[i] <- "LO"

What is the problem you are trying to solve?


Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.

On Wed, Jul 15, 2015 at 6:26 AM, Luigi Marongiu 
wrote:

> Dear all,
> I have a vector that comes from some calculations I am making and this
> vectors turns out to be in character format rather than numerical. I
> can convert it in numerical format but then the calculations are not
> correct, as you can see in the following example. I was also expecting
> that rounding a number such as 5.43 to a three digits one would return
> 5.430 but that did not happen. Any tips on how to apply the
> calculation correctly?
> Thank you
> best regards
> luigi
>
> >>>
> vec.ori <- c("5.43", "6.63", "-1.18593063116494e+36", "6.2", "5.61",
> "4.96842801255869e+30", "5.59", "-Inf", "Inf", "5.49", "18.35",
> "-3.11", "6.07", NA)
>
> vec.num <- as.numeric(vec.ori)
>
> vec.num <0
>
> [1] FALSE FALSE  TRUE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE
> TRUE FALSENA
>
> vec.num >0
>
>  [1]  TRUE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE
> FALSE  TRUENA
>
> for(i in 1:length(vec.num)) {
>   cat("value at beginning: ", vec.num[i], "\n", sep="")
>   if(vec.num[i] < 0) {
> vec.num[i] <- "LO"
>   } else if(vec.num[i] > 45) {
> vec.num[i] <- "HI"
>   } else if (is.na(vec.num[i])== TRUE) {
> vec.num[i] <- "na"
>   } else if (is.infinite(vec.num[i]) == TRUE) {
> vec.num[i] <- "INF"
>   } else {
> vec.num[i] <- round(vec.num[i], 3)
>   }
>   cat("value at end: ", vec.num[i], "\n", sep="")
> }
>
> value at beginning: 5.43
> value at end: 5.43
> value at beginning: 6.63
> value at end: 6.63
> value at beginning: -1185930631164940020264024442864400022
> value at end: LO   # REM: error!
> value at beginning: 6.2
> value at end: HI   # REM: error!
> value at beginning: 5.61
> value at end: HI   # REM: error!
> value at beginning: 4968428012558689723622822000404
> value at end: HI
> value at beginning: 5.59
> value at end: HI   # REM: error!
> value at beginning: -Inf
> value at end: LO   # REM: error!
> value at beginning: Inf
> value at end: HI   # REM: error!
> value at beginning: 5.49
> value at end: HI   # REM: error!
> value at beginning: 18.35
> Error in round(vec.num[i], 3) :
>   non-numeric argument to mathematical function
> # REM: cycle crashed
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.