简体   繁体   中英

How do I put numbers with different roundings in a matrix?

I'm trying to place numbers in the same column of a matrix with different roundings. However, whenever I try, the numbers with the less precise roundings have a bunch of zeroes tacked on. How do I fix this?

> b <- matrix(nrow=2, ncol = 2)
> b[1,1] <- 20
> b[2,1] <- 4.42
> b
      [,1] [,2]
[1,] 20.00   NA
[2,]  4.42   NA

That is because you are combining a bunch of integers with a bunch of double s (think decimal points). Whenever you combine two different type s of values in R, it will bring them all up to one type. In this case double s. That is why your 20 looks like 20.00. Simple answer is you can't have both 20 and 4.42. Plus, why would you want to?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM