vendredi 22 février 2019

I'm trying to add ".0" onto any single digit numbers in my vector

Can someone please explain why this code isn't working? I want the 'nums' character vector to output "4.4" "" "5.0" but i'm getting an error message. Something about a do.call()

In addition to knowing why it's doing this can someone perhaps provide an easier way of turning the "5" into "5.0" but leaving the other numbers as they are. Please keep in mind i actually have many numbers so something like nums[3] <- "5.0" wouldn't be useful.

y <- 0
nums <- c("4.4", "", "5")

for(x in nums){

y <- 0 + 1

if(nchar(x) == 1){
  nums[y] <- paste(x, ".0", sep = "")
}else{}
}

Error in do.call(base::call, c(name = "{", else_lines), quote = TRUE) : second argument must be a list

Aucun commentaire:

Enregistrer un commentaire