This is a question I had while going through R programming course from Coursera. I had asked this question in their forums, but didn't get any answer.
So I thought, I should ask it here.
As I understand what Professor was talking about in that lecture - by default, when we store any number value in variable such as shown below
x <- 1
x
# prints numeric
class(x)
But why is it that when we store a vector such as shown below (note: still without the 'L' symbol to force it as an integer)
x <- 1:10
x
# prints "integer", but why?
class(x)
I thought it should give me a numeric vector, but it is not the case.
Can anybody please explain what is happening here?
this has been discussed, see http://r.789695.n4.nabble.com/Integer-vs-numeric-td847329.html
From help(":")
Value:
For numeric arguments, a numeric vector. This will be of type
'integer' if 'from' is integer-valued and the result is
representable in the R integer type, otherwise of type '"double"'
(aka 'mode' '"numeric"').
Related
As stated above, I'm trying to convert data in my dataframe from integer/dbl to numeric but I end up with dbl for both columns.
Original dataset
Code I'm using to convert to numeric;
data$price <- as.numeric(data$price)
data$lot_size <- as.numeric(data$lot_size)
The dataframe I end up with:
The dataframe I end up with
Dataset I have been working with: https://dasl.datadescription.com/datafile/housing-prices-ge19
"numeric is identical to double"
https://stat.ethz.ch/R-manual/R-devel/library/base/html/numeric.html
> typeof(as.numeric(3L))
[1] "double"
> typeof(as.integer(3L))
[1] "integer"
The stuff with types in R is a bit confusing. I would say that numeric is not really a data type at all in R. You will never get the answer numeric from the typeof function.
Both, integers and doubles are considered to be numeric and the function is.numeric will return TRUE for either.
On the other hand, numeric is more often a synonym for double.
The functions numeric and as.numeric are the same as double and as.double.
Edit:
With a bit more research under my belt let me rephrase it like this:
'numeric' is the virtual superclass of both integer and double.
See for example getClass("numeric") and help(UseMethod) (first paragraph in the Details section).
Hadley says it better: Advanced R
I have a data.frame from which I extracted a column called Volume. The code is as follows:
volume = aapl.us$Volume
In the console, I am told the following:
typeof(volume)
# "integer"
length(volume)
# 8364
How is this possible?
The case that you encounter is not strange behavior in R. It may sound unintuitive at first to users of other programming language where there is a distinction between a scalar (single number) and a vector (one-dimensional array).
R does not have "scalar" data. Simplest data structure in R is a vector, and it can be a numeric, character, factor, integer, logical, or complex-valued vector. A single number in R is a "vector of length one", and not a "scalar". A vector must contain data of the same type.
typeof() returns the type of a variable (see the link for further information). In your case, Volume is a vector that contains integers, and that vector has length 8364.
I was trying to coerce an integer to numeric using as and found the following:
str(as(1L, "numeric"))
# int 1
str(as.numeric(1L))
# num 1
str(as(1L, "double"))
# num 1
I am expecting the first to be a numeric type as well. Why does it not return a numeric vector? Do I need match.fun("as.numeric") instead?
As there are many questions on as.numeric, I am unable to find a dupe for this.But if there is, I am very happy to close the question.
Using R-3.6.1 Win x64
From ?double
The potential confusion is that R has used mode "numeric" to mean
‘double or integer’
From ?numeric
as.numeric is a generic function, but S3 methods must be written for
as.double. It is identical to as.double.
Basically, nothing's changed when you do as(1L, "numeric") because 1L is already numeric.
For whatever reason, as.numeric seems to be identical to as.double.
Things may be different for S4.
Hi I am trying to convert my column within the data frame from "double" to a "factor", but its not working
I am trying to convert the "double" data type to "factor" but its converting it to an integer. I have tried a couple of other things from stackoverflow but nothing seems to work. I have provided my code below along with console output.
Task 1.5 - Change class type from Integer to Factor
typeof(iLPdf$class) #check type
iLPdf$class <- as.factor(iLPdf$class)
typeof(iLPdf$class) #check type
[1] "double"
iLPdf$class <- as.factor(iLPdf$class)
typeof(iLPdf$class) #check type
[1] "integer"
The issue here is that typeof checks the internal representation of an object. Factors are represented as integers. To check that something is actually a factor, use is.factor instead. From the docs:
typeof determines the (R internal) type or storage mode of any object
To verify this "claim", you can check the well known iris Species' column which is a factor. typeof(iris$Species) will however return integer because to R factors are integers.
Using is.factor is a better option, this ultimately boils down to the difference between types and classes in R.
is.factor(iris$Species)
[1] TRUE
Starting to learn R, and I would appreciate some help understanding how R decides the class of different vectors. I initialize vec <- c(1:6) and when I perform class(vec) I get 'integer'. Why is it not 'numeric', because I thought integers in R looked like this: 4L
Also with vec2 <- c(1,'a',2,TRUE), why is class(vec2) 'character'? I'm guessing R picks up on the characters and automatically assigns everything else to be characters...so then it actually looks like c('1','a','2','TRUE') am I correct?
Type the following, you can see the help page of the colon operator.
?`:`
Here is one paragraph.
For numeric arguments, a numeric vector. This will be of type integer
if from is integer-valued and the result is representable in the R
integer type, otherwise of type "double" (aka mode "numeric").
So, in your example c(1:6), since 1 for the from argument can be representable in R as integer, the resulting sequence becomes integer.
By the way, c is not needed to create a vector in this case.
For the second question, since in a vector all the elements have to be in the same type, R will automatically convert all the elements to the same. In this case, it is possible to convert everything to be character, but it is not possible to convert "a" to be numeric, so it results in a character vector.