-1
print(krit)
  names weight
1   may     36
2 mayer     49
3  mayo     35
4  mali     50
> mean(krit$weight)
[1] 42.5
> typeof(weight)
[1] "double"
> typeof(names)
[1] "character"

Why is the weight column a double vector and not integer vector? At what point do you specify a vector should be an integer or double? Does this pose any sign towards data analysis with R?

4
  • Welcome to StackOverflow! Please read How to Ask and edit your post to more clearly define exactly what your question is. At the moment it's not very clear what you're asking Commented Nov 18 at 15:42
  • 1
    Some more background here: r4ds.had.co.nz/vectors.html#numeric. R defaults to doubles. You can specify a single number as an integer by adding an L afterwards, and you can coerce a double vector to integer using as.integer(). Commented Nov 18 at 15:54
  • the differences between double/numeric/integer almost never matters, i cant think of a case off hand unless you did something like as.integer(1.5) where you should be rounding explicitly Commented Nov 18 at 16:05
  • Floating point numbers are great for most data analysis situations, and are used in almost every programming language. The main way people run into trouble is when testing for exact equality between finite decimals, as floating point numbers can't express every number exactly, even some simple ones like 0.1. So some care should be taken there. See stackoverflow.com/questions/9508518/… Commented Nov 18 at 20:23

1 Answer 1

1

In R, "double" or "integer" describes how the number is stored. The integer type can only store whole numbers, while the double type can store a large number of fractional values (but not all of them: only fractions where the denominator is a power of 2 within a specific range). The values that can be stored as an integer are strictly a subset of the values that can be stored as a double (since 2^0 = 1 can be the denominator of the fraction in the double).

Since most arithmetic in R needs fractional values, most numeric vectors use the double storage format.

For data analysis, this hardly ever matters. You can usually ignore the difference.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.