Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

516 object vs variable #523

Merged
merged 2 commits into from
Sep 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 19 additions & 18 deletions episodes/01-intro-to-r.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
::::::::::::::::::::::::::::::::::::::: objectives

- Define the following terms as they relate to R: object, assign, call, function, arguments, options.
- Assign values to objects in R.
- Assign values to names in R.
- Learn how to name objects.
- Use comments to inform script.
- Solve simple arithmetic operations in R.
Expand All @@ -37,7 +37,7 @@

- What data types are available in R?
- What is an object?
- How can values be initially assigned to variables of different data types?
- How can objects of different data types be assigned to names?
- What arithmetic and logical operators can be used?
- How can subsets be extracted from vectors?
- How does R treat missing values?
Expand All @@ -54,15 +54,15 @@
12 / 7
```

However, to do useful and interesting things, we need to assign *values* to
*objects*. To create an object, we need to give it a name followed by the
assignment operator `<-`, and the value we want to give it:
Everything that exists in `R` is an objects: from simple numerical values, to strings, to more complex objects like vectors, matrices, and lists. Even expressions and functions are objects in `R`.

However, to do useful and interesting things, we need to name objects. To do so, we need to give a *name* followed by the assignment operator `<-`, and the *object* we want to be named:

```{r, purl=FALSE}
area_hectares <- 1.0
```

`<-` is the assignment operator. It assigns values on the right to objects on
`<-` is the assignment operator. It assigns values (objects) on the right to names (also called *symbols*) on
the left. So, after executing `x <- 3`, the value of `x` is `3`. The arrow can
be read as 3 **goes into** `x`. For historical reasons, you can also use `=`
for assignments, but not in every context. Because of the
Expand All @@ -83,7 +83,7 @@
(e.g., `age` is different from `Age`). There are some names that
cannot be used because they are the names of fundamental objects in R (e.g.,
`if`, `else`, `for`, see
[here](https://stat.ethz.ch/R-manual/R-devel/library/base/html/Reserved.html)

Check warning on line 86 in episodes/01-intro-to-r.Rmd

View workflow job for this annotation

GitHub Actions / Build markdown source files if valid

[uninformative link text]: [here](https://stat.ethz.ch/R-manual/R-devel/library/base/html/Reserved.html)
for a complete list). In general, even if it's allowed, it's best to not use
them (e.g., `c`, `T`, `mean`, `data`, `df`, `weights`). If in
doubt, check the help to see if the name is already in use. It's also best to
Expand All @@ -108,15 +108,16 @@

## Objects vs. variables

What are known as `objects` in `R` are known as `variables` in many other
programming languages. Depending on the context, `object` and `variable` can
The naming of objects in `R` is somehow related to `variables` in many other
programming languages. In many programming languages, a variable has three aspects: a name, a memory location, and the current value stored in this location. `R` abstracts from modifiable memory locations. In `R` we only have objects which cn be named.
Depending on the context, `name (of an object)` and `variable` can
have drastically different meanings. However, in this lesson, the two words
are used synonymously. For more information see:
[https://cran.r-project.org/doc/manuals/r-release/R-lang.html#Objects](https://cran.r-project.org/doc/manuals/r-release/R-lang.html#Objects)

::::::::::::::::::::::::::::::::::::::::::::::::::

When assigning a value to an object, R does not print anything. You
When assigning an value to a name, R does not print anything. You
can force R to print the value by using parentheses or by typing
the object name:

Expand All @@ -133,22 +134,22 @@
2.47 * area_hectares
```

We can also change an object's value by assigning it a new one:
We can also change an the value assigned to an name by assigning it a new one:

```{r, purl=FALSE}
area_hectares <- 2.5
2.47 * area_hectares
```

This means that assigning a value to one object does not change the values of
other objects. For example, let's store the plot's area in acres
in a new object, `area_acres`:
This means that assigning a value to one name does not change the values of
other names. For example, let's name the plot's area in acres
`area_acres`:

```{r, purl=FALSE}
area_acres <- 2.47 * area_hectares
```

and then change `area_hectares` to 50.
and then change (reassign) `area_hectares` to 50.

```{r, purl=FALSE}
area_hectares <- 50
Expand All @@ -158,7 +159,7 @@

## Exercise

What do you think is the current content of the object `area_acres`? 123.5 or
What do you think is the current value of `area_acres`? 123.5 or
6\.175?

::::::::::::::: solution
Expand Down Expand Up @@ -244,7 +245,7 @@

Here, the value of `a` is given to the `sqrt()` function, the `sqrt()` function
calculates the square root, and returns the value which is then assigned to
the object `b`. This function is very simple, because it takes just one argument.
the name `b`. This function is very simple, because it takes just one argument.

The return 'value' of a function need not be numerical (like that of `sqrt()`),
and it also does not need to be a single item: it can be a set of things, or
Expand Down Expand Up @@ -326,7 +327,7 @@
either numbers or characters. We can assign a series of values to a vector using
the `c()` function. For example we can create a vector of the number of household
members for the households we've interviewed and assign
it to a new object `hh_members`:
it to `hh_members`:

```{r, purl=FALSE}
hh_members <- c(3, 7, 10, 6)
Expand All @@ -343,7 +344,7 @@
```

The quotes around "muddaub", etc. are essential here. Without the quotes R
will assume there are objects called `muddaub`, `burntbricks` and `sunbricks`. As these objects
will assume there are objects called `muddaub`, `burntbricks` and `sunbricks`. As these names
don't exist in R's memory, there will be an error message.

There are many functions that allow you to inspect the content of a
Expand Down
Loading