Update index.md (#22824)
Edited for clarity, added clarification on the assignment operators and gave examples of types of objects.
This commit is contained in:
committed by
Christopher McCormack
parent
cbb7ffb29e
commit
57d31d2c48
@ -3,25 +3,42 @@ title: Objects in R
|
|||||||
---
|
---
|
||||||
## Objects
|
## Objects
|
||||||
|
|
||||||
R allows to save the data by storing it inside an R object.
|
R allows the user to save the data by storing it inside an object.
|
||||||
|
|
||||||
## What’s an object?
|
## What’s an object?
|
||||||
|
|
||||||
It is just a name that you can use to call up stored data. For example, you can save data into an object like a or b.
|
An object is a name that you can use to call up stored data. For example, you can save data into an object like a or b.
|
||||||
```r
|
```r
|
||||||
> a <- 5
|
> a <- 5
|
||||||
> a
|
> a
|
||||||
[1] 5
|
[1] 5
|
||||||
```
|
```
|
||||||
|
|
||||||
|
There are many types of objects in R. Two of the most common are as lists and functions.
|
||||||
|
Example of a list:
|
||||||
|
```
|
||||||
|
> a <- list(1, "Hello, world!", FALSE)
|
||||||
|
```
|
||||||
|
Lists can contain mixed types of data (number, string and boolean in this example).
|
||||||
|
|
||||||
|
There are many built in functions in R. The user can also make a 'user defined' function, as you see below.
|
||||||
|
The following creates the function circleArea that calculates the area of a circle (A = π r2).
|
||||||
|
|
||||||
|
```
|
||||||
|
>circleArea <- function(r){
|
||||||
|
A <- pi * r^2
|
||||||
|
return(A)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
## How to create an Object in R?
|
## How to create an Object in R?
|
||||||
|
|
||||||
1. To create an R object, choose a name and then use the less-than symbol, `<`,
|
1. To create an R object, choose a name and then use the less-than symbol, `<`,
|
||||||
followed by a minus sign, `-`, to save data into it. This combination looks like an
|
followed by a minus sign, `-`, to save data into it. This combination looks like an
|
||||||
arrow, `<-`. R will make an object, give it your name, and store in it whatever
|
arrow, `<-`. R will make an object, give it your name, and store in it whatever
|
||||||
follows the arrow.
|
follows the arrow. You can also use the '=' sign, but this is a less common method to assign a value to an object and generally frowned upon.
|
||||||
|
|
||||||
2. When you ask R what’s in a, it tells you on the next line. For example:
|
2. When the user calls the object in the console it provides the output on the next line. For example:
|
||||||
|
|
||||||
```r
|
```r
|
||||||
> die <- 1:6
|
> die <- 1:6
|
||||||
@ -38,7 +55,6 @@ Second, a name cannot use some special symbols, like `^, !, $, @, +, -, /
|
|||||||
5. You can see which object names you have already used with the function `ls()`.
|
5. You can see which object names you have already used with the function `ls()`.
|
||||||
|
|
||||||
## References
|
## References
|
||||||
|
- [Official Docs](https://cran.r-project.org/manuals.html)
|
||||||
* [Official Docs](https://cran.r-project.org/manuals.html)
|
- [Objects in R by r-bloggers](https://www.r-bloggers.com/classes-and-objects-in-r/)
|
||||||
* [Objects in R by r-bloggers](https://www.r-bloggers.com/classes-and-objects-in-r/)
|
- [CRAN](https://cran.r-project.org/doc/manuals/r-release/R-lang.html)
|
||||||
* [CRAN](https://cran.r-project.org/doc/manuals/r-release/R-lang.html)
|
|
||||||
|
Reference in New Issue
Block a user