---
title: "Ling 120, session 01: R basics 1"
author:
   - name: "[Stefan Th. Gries](https://www.stgries.info)"
     affiliation:
        - UC Santa Barbara
        - JLU Giessen
     orcid: 0000-0002-6497-3958
date: "2027-01-05 12:34:56"
date-format: "DD MMM YYYY HH-mm-ss"
editor: source
format:
   html:
      page-layout: full
      code-fold: false
      code-link: true
      code-copy: true
      code-tools: true
      code-line-numbers: true
      code-overflow: wrap
      number-sections: true
      smooth-scroll: true
      toc: true
      toc-depth: 4
      number-depth: 4
      toc-location: left
      mainfont: Times New Roman
      monofont: Lucida Console
      tbl-cap-location: top
      fig-cap-location: bottom
      fig-width: 6
      fig-height: 6
      fig-format: png
      fig-dpi: 300
      fig-align: center
      embed-resources: true
      include-in-header:
         text: '<link rel="shortcut icon" href="https://www.stgries.info/favicon.ico" type="image/x-icon" />'
execute:
   cache: false
   echo: true
   eval: true
   warning: false
---

We clear the workspace and load a package I will often use later -- if you get an error message there, you may have to install the package like shown here:

```{r prepworkspace}
rm(list=ls(all=TRUE)); library(magrittr)
# if error, run the next line (w/out the comment)
# install.packages("magrittr")
```

# Functions and arguments

Functions are instructions to R to do something; arguments are (i) what the instruction is to be applied to and (ii) how the instruction is to be applied to it. We can learn what arguments a function takes and what their default settings are by using the function `args` and giving it the name of a function as an argument:

```{r args}
args(log)
```

The output shows several important things about the arguments of functions in general and of course about `log` in particular. Apparently, `log` can take two arguments:

* the first argument is the number you want to log; internally to the function `log`, that argument is called `x` and since `x` is not defined in the function itself, it must be provided by the user (obviously, R cannot possibly guess let alone know which number(s) we want log);
* the second argument is the base of the logarithm we want to use; internally to the function `log`, that argument is called `base` and that one *is* defined in the function itself (namely as `exp(1)`, which is `r exp(1)`), which means the user can either accept R's default log setting of `base` (because that might be what is needed) or the user can override R's default setting of `base` with their own desired one (e.g., if we want the logarithm to the base of 2 or 10).

Thus, if we want the natural log of 5, the most explicit way to get it would be like this. Note, I am first presenting the short form as a one-liner (used a lot in the book to save space) ...

```{r logshort}
# short form
log(x=5, base=exp(1))
```

... and then the long form (used in the code files so I could add comments after `#` for each argument of a function call to explain things.)

```{r loglong}
# long form, broken down by arguments
log(            # compute the log
   x=5,         # of 5
   base=exp(1)) # to the base of e
```

However, since we're using the default setting of the `base` argument, R actually allows us to omit it: If we do not override R's default setting of an argument (because that's what we want), then R will use it:

```{r logdefault}
log(x=5) # compute the log of 5 (to the base of e)
```

In fact, this can be done even shorter: If we provide the arguments to a function in exactly the order in which R expects them -- which is the order we saw when we ran `args(log)`: first `x` (the number to log), second, maybe, `base` (the base of the logarithm) -- we don't even need to name the variables in the function call:

```{r logdefaultshort}
log(5) # compute the log of 5 (to the base of e)
```

Note that I will, where it’s didactically useful, use `magrittr`’s pipe `%>%` to make code easier to read. For those who don’t know, the pipe allows you to adopt a more human reader-friendly sequencing of functions and arguments. Reducing things to the absolute basics, `x %>% f` is equivalent to `f(x)`:

```{r pipe1}
5 %>% sqrt # same as sqrt(5)
## [1] 2.236068
8 %>% log  # same as log(8)
## [1] 2.079442
```

Note: the pipe makes the thing before it (often called `lhs`, for 'left-hand side') the first argument of the thing/function after it (often called `rhs`, for, you're not gonna believe this, 'right-hand side'). But of course the `rhs` function might allow or require more arguments than just one, like `log` allows a second argument for the base of the log. If you give the `rhs` more arguments, R will either treat them as the second, the third, etc. argument, or it will use any names you might provide. Thus, the following are two ways of using the pipe to compute the ternary log of 8:

```{r pipe2}
#| collapse: true
8 %>% log(base=3) # because the 8 is made ...
8 %>% log(3)      # ... the 1st arg of log
```

But sometimes there are workflow situations where the `lhs` does not correspond to the first argument of the function in the `rhs`. In that case, you can use a dot `.` to tell R where in the `rhs` the `lhs` from before the pipe `%>%` is to be inserted. For example, in the following lines, the dot says 'make the `lhs` of 2 the second/`base` argument of `log`:

```{r pipe3}
#| collapse: true
2 %>% log(8, base=.)
2 %>% log(8, .)
```

Crucially, we can chain multiple function calls, and this is where readability can benefit most:

```{r pipe4}
#| collapse: true
#| results: hold
sqrt(log(5)) # shorter, but arguably harder to process than
5 %>% log %>% sqrt # this, where the order of functions
# corresponds to the order in which things are done
```

Some other functions that are often useful:

* `sign` takes a numeric vector `x` as its only argument and returns the sign(s) of its element(s):
   + -1 when the element is <0;
   + 0 when the element is 0;
   + 1 when the element is >0;
* `round` takes a numeric vector `x` and a numeric vector `digits` as its arguments and rounds the elements of `x` to `digits` decimal places (NB: according to [IEC 60559 standard](https://en.wikipedia.org/wiki/IEEE_754#Rounding_rules); see what happens if you try `round(12.5)`);
* `min` takes 1+ numeric vectors as its initial arguments (and `na.rm` as its last argument, to ignore missing data) and returns the smallest value of all numeric vectors;
* `max` takes 1+ numeric vectors as its initial arguments (and `na.rm` as its last argument, to ignore missing data) and returns the largest value of all numeric vectors;
* `sum` takes 1+ numeric vectors as its initial arguments (and `na.rm` as its last argument, to ignore missing data) and returns the sum of all numeric vectors.

Finally, note that what looks like plain ol' operators can be used as functions in R as well, which sometimes also makes for easier-to-read or to comment code:

```{r opsASfuncs}
"+"(2, 3) # same as 2+3
"-"(2, 3) # same as 2-3
"*"(2, 3) # same as 2+3
"/"(2, 3) # same as 2/3
```

# Data structures 1: vectors

The most basic data structure in R is a vector. Vectors are one-dimensional, sequentially ordered sequences of elements such as numbers or character strings (such as words).

## Creating vectors

Vectors can be generated in R in a variety of ways. The simplest way is to take the name you want a vector to have (e.g. `aa`) and put into it `<-` (the 'assignment arrow') what you want the vector to contain (e.g. the number 5); the following creates a numeric vector `aa`:

```{r createvector1}
# creating a single-element numeric vector
aa <- 5
```

Now you can check whether this worked and 'print' the contents of `aa`:

```{r outputvector}
aa
```

Often, it is useful to parenthesize an assignment because that will immediately print the created object:

```{r parenthassign}
(aa <- 5) # short for aa <- 5; aa
```

Typically, we want to work with longer vectors, which we can create with the function `c`:

```{r createvector2}
# creating a multi-element numeric vector from 3 single-element vectors
(bb <- c(3, 5, 7))
# creating a multi-element character vector from 3 single-element vectors
(cc <- c("impetus", "klumpatsch", "facetiously"))
```

And of course we can combine not just single-element vectors like above but also multi-element vectors:

```{r createvector3}
# generate two vectors
numbers1 <- c(1, 2, 3)
numbers2 <- c(4, 5, 6)
# combine them
(numbers1.and.numbers2 <- c(numbers1, numbers2))
```

### Exercises 1

Use R functions to

1: compute the maximum of all numbers in `bb`, `numbers1`, and `numbers2`:

```{r exercises1_1}
max(bb, numbers1, numbers2)
```

2: compute the sum of all numbers in `bb`, `numbers1`, and `numbers2`:

```{r exercises1_2}
sum(bb, numbers1, numbers2)
```

3: What does the following suggest the function `prod` does?

```{r exercises1_3}
prod(bb, numbers2) # multiplies all elements of all argument vectors
```

## Back to creating vectors: `rep` & `seq`

The next function we look at is `rep` (for "repetition"). In the simplest scenario, the function takes two arguments:

* the first argument is internally called `x` and refers to the vector whose elements are to be repeated;
* the second argument is
   + either a numeric vector `times`, which states how often all of `x` is to be repeated;
   + or a numeric vector `each`, which states how often each element of `x` is to be repeated;
   + or a numeric vector `length.out`, which states how long the resulting vector should be.

Here are some straightforward examples:

```{r rep1}
#| collapse: true
rep(x="something", times=3)
rep(x="something", each=3)
rep(x=4, length.out=3)
```

But remember: `x` is a vector, but nothing says it can only be a single-element vector as above and now we see the difference between `times` and `each`:

```{r rep2}
#| collapse: true
rep(x=c("anim", "inanim"), times=3)
rep(x=c("anim", "inanim"), each=3)
```

In the first case, R repeats all of `x` three times; in the second, R repeats each element of `x` three times before it goes on to the next. And here's an example of `rep` last possible second argument, `length.out`:

```{r replengthout}
rep(x=c(1, 2, 3), length.out=10)
```

R repeats the elements of `x` over and over again till it runs into the desired output length -- then it stops right away, even if that means that not everything gets repeated equally often (much much later, some of you might use that for cross-validation).

Finally, can you guess what this will do, i.e. what happens if the `times` argument is a multi-element vector?

```{r reppairwise}
rep(x=c(4, 5, 6), times=c(3, 2, 1))
```

This maps the elements of `times` onto the elements of `x` in a pairwise fashion and repeats them accordingly: The first element of `x` (4) is repeated as often as the first element of `times` states (3), etc. This pairwise mapping is something you need to bear in mind because of how useful and prevalent it is. In fact, it is very useful for basic arithmetic operations; look, this first example shouldn't surprise you: pairwise mapping again:

```{r manytimesmany}
c(2, 3, 4) * c(5, 6, 7)
```

But what happens now?

```{ manytimesone}
c(2, 3, 4) * 5
```

R pairs up the two first elements, the 2 with the 5. Then, it wants to pair up the second elements so it takes the 3, but then there is no second element in the second vector, so R begins with the first again and pairs up 3 and 5, and then the same with the 4 and the 5, etc. In other words, the 5 gets recycled as often as necessary to get through the multiplication.

The final vector generation function we look at is `seq` (for "sequence"). Maybe a bit confusingly, it has four main uses; I will begin with the first two, which are similar to the uses of `rep`. These involve the following arguments:

* the first argument is called `from` and is a numeric single-element vector denoting the starting point of a numeric sequence;
* the second argument is called `to` and is a numeric single-element vector denoting the end point of a numeric sequence;
* the third argument is
   + either a numeric vector `by` in which case it states the increment which you add to `from` as often as needed to get from `from` to `to`;
   + or a numeric vector `length.out`, in which case it states how long the resulting vector should be (and R figures out for you what `by` needs to be for that).

The following examples should make this clear:

```{r sepfromtolen}
#| collapse: true
# using by
seq(from=2, to=6, by=0.4)
seq(from=6, to=2, by=-0.4)
# using length.out
seq(from=2, to=6, length.out=4)
seq(from=6, to=2, length.out=4)
```

The other two uses of `seq` I mention here involve what happens if you give it just a single argument and those are often used in programming applications involving so-called `for`-loops:

* if that argument is a positive integer `qwe`, `seq` generates a sequence from 1 to that integer with `by` being 1, i.e. `1:qwe`;
* if that argument is a multi-element data structure `asd`, `seq` generates a sequence from 1 to however many elements that data structure has with `by` being 1; in other words, it does `seq(length(asd))`.

The following examples should make this clear again:

```{r seponearg}
#| collapse: true
# using positive integers
seq(3) # same as seq(from=1, to=3, by=1)
# using multi-element vectors
seq(c(2,5,8)) # same as seq(from=1, to=length(c(2, 5, 8)), by=1)
```

Another useful feature of vectors is that you can not only name the whole vectors, but also their individual elements:

```{r names}
numbers <- c(1, 2, 3); names(numbers) <- c("one", "two", "three")
numbers
```

A shorter way to do this would be this:

```{r setNames}
(numbers <- setNames(
   object=c(1, 2, 3),            # the vector content
   nm=c("one", "two", "three"))) # its names
```

And why would one want to do that? That will be one of the topics of the next section -- I'm sure you can hardly wait ...

### Exercises 2

Use R functions to

1: to generate the following vector (use rep): 1,2,2,3,3,3,4,4,4,4:

```{r exercises2_1}
rep(1:4, 1:4)
```

2: create a vector weights as follows: Imagine you weighed yourself on Monday and weighed 170 lbs, then you weigh yourself on Sun and weigh 160 lbs -- use seq to interpolate the weights on the days you didn't weigh yourself, put all the weights into one vector called weights, round it to two decimals, and give it weekday names:

```{r exercises2_2}
weights <- setNames(
   object=seq(from=170, to=160, length.out=7) %>% round(2),
   nm=c("Mon","Tue","Wed","Thu","Fri","Sat","Sun"))
```

## Various functions & operators

The most important way to access parts of a vector (or other data structures) in R involves subsetting with **square brackets**. There are two main ways to do that:

* we can subset a vector **with a logical vector of the same length**, where that logical vector has `TRUE`s for the elements we do want and `FALSE`s for the elements we don't want;
* we can subset a vector **with a numeric vector** that lists only the positions of elements we want.

In the simplest form, for example, this is how these two ways would allow us to access an individual vector element (here, the third):

```{r subsetlognum1}
cc <- c( "a"  , "b"  , "c" , "d"  , "e")   # the spacing is not required, it's
cc[   c(FALSE, FALSE, TRUE, FALSE, FALSE)] # to highlight the correspondences
cc[3]         # show the 3rd element of cc
y <- 3; cc[y] # show the y-th (i.e., here, 3rd) element of cc
```

Since you already know how flexible R is with vectors, the following uses involving multi-element subsetting should not come as big surprises:

```{r subsetlognum2}
cc[c(TRUE, FALSE, TRUE, FALSE, FALSE)]
cc[c(3, 1)]

cc[c(TRUE, TRUE, TRUE, FALSE, FALSE)]
cc[c(1:3)]
```

Also, note, that subsetting with square brackets is another one of these things that looks like an operator or 'syntax' but is actually also a function:

```{r subsetASfunc}
"["(cc, c(3, 1))
```

With negative numbers, you can leave out elements:

```{r subsetnegnum}
cc[-2]
```

However, there are many more powerful ways to access parts of vectors and they involve letting R determine which elements of a vector fulfill a certain condition. One way to do this involves presenting R with a **logical expression** so that, instead of typing a logical vector yourself like above, R returns a logical vector for you. You should read the following with a question intonation to realize how close to English R can be: `cc` is *d*?

```{r logexpr1}
cc=="d"
```

R checks for each individual element of `cc` whether it is *d* or not and returns all individual finding together in a logical vector of `FALSE`s (*no*s) and `TRUE`s (*yes*s). The only thing requiring a little attention here is that the logical expression uses **two equal signs**, which distinguishes logical expressions from assignments such as `file=""`, which obviously use only one. Some other logical operators are:

* `&` means 'and' and `|` means 'or';
* `>`, `>=`, `<`, and `<=` mean what you think they mean;
* `!` means 'not' and `!=` means 'not equal to', i.e. &ne;.

Here are some examples that involve logical vectors of `TRUE`s and `FALSE`s:

```{r logexpr2}
#| collapse: true
(bb <- 10:1) # create a vector to 'play with'
bb==4         # is bb 4?
bb<=7         # is bb <=7?
bb!=8         # is bb not 8?
(bb>8 | bb<3) # is bb >8 or <3?
```

Another way to do this involves the function `which`, which takes a logical vector as its first argument `x` (and of course you can type that yourself or have R create it from a logical expression) and returns the positions of the `TRUE`s. Thus, `which` translates the first way of subsetting (with logical vectors) into the second (with numeric position indices of `TRUE`s).

```{r logANDwhich}
#| collapse: true
bb # just to remind you what bb is
which(bb==4)       # which position(s) of bb (if any) contains a 4?
which(bb<=7)       # which position(s) of bb (if any) contains values <=7?
which(bb!=8)       # which position(s) of bb (if any) contains values that are not 8?
which(bb>8 | bb<3) # which position(s) of bb (if any) contains values >8 or <3?
```

The above code involved examples where you tested whether each element of `bb` is one particular thing (4) and we did that with `bb==4` . What if we have several things to check `bb` for? For that, the operator `%in%` is useful and typically it gets used like this:

```{r in}
c(1, 6, 11) %in% bb # is 1 in bb? is 6? is 11? ...
```

This checks for each element of the vector to the left of `%in%` whether it is attested in the vector to the right of `%in%`: 1 and 6 are in `bb`, 11 is not. (By the way, if you want to look up the help file for `%in%`, you can't say `?%in%`, you have to use `` ?`%in%` ``.) There are also set operation functions such as these:

```{r sets}
union(3:6, 2:7)
intersect(3:6, 2:7)
setdiff(3:6, 2:5)
setdiff(2:5, 3:6)
```

If you would also like to know the exact position of the first (!) occurrence of each of the elements of the first vector (called `x`) in the second vector (called `table`), you can use `match`:

```{r match}
match(            # where do the elements
   x=c(1, 6, 11), # 1, 6, 11 show up in
   table=bb)      # bb for the 1st time?
```

That is to say, the first element of the first vector (the 1) occurs the first time at the tenth position of `bb`; the second element of the first vector (the 6) occurs the first time at the fifth position of `bb`; the last element of the first vector (the 11) doesn't occur in `bb`. (The function `match` might remind you of VLOOKUP in spreadsheet software.)

Another useful function is `unique`, which can be explained particularly easily to linguists: It goes through all the elements of a vector (the tokens) and returns all elements that occur at least once (the types) in their order of first occurrence:

```{r unique}
#| collapse: true
# I am using the inbuilt vector letters here (see ?letters)
(qwe <- letters[c(1, 3, 2, 2, 3, 4, 3, 4, 5)])
unique(x=qwe) # what are the unique types in qwe?
```

But this doesn't tell us how often each of the types is attested, for which we can use the function `table`, which returns the frequencies of all types (in alphabetical order):

```{r table}
table(qwe) # how often does qwe contain which type?
```

In words, `qwe` contains *a* 1 time, *b* 2 times, etc.; this is an extremely useful function for corpus processing with R. (Note, extremely important: `table` by default ignores missing data -- if you want to count those, too, one option to get those is using `table(..., useNA="ifany")`.) Crucially, the values that `table` returns are the frequencies (1, 2, 3, 2, 1) and these frequencies have the names of the types they refer to ("a", "b", "c", "d", "e") This should remind you of what I told you earlier: one can name vector elements, which above we did with this bit of code:

```{r namesagain}
#| results: hold
numbers <- c(1, 2, 3); names(numbers) <- c("one", "two", "three"); numbers
# or:
# numbers <- setNames(
#    c(1, 2, 3),
#    c("one", "two", "three"))
```

One extremely useful thing is that one can then not only access parts of tables or vectors in the ways discussed above -- with logical vectors and numeric position vectors -- but now also with the names. Check this out:

```{r tablesHAVEnames}
#| collapse: true
qwe_t <- table(qwe)
qwe_t["b"] # & what's the frequency of "b" in the vector qwe?
numbers["two"] # what is the number stored in the slot of numbers called "two"?
```

This becomes interesting when you have a table called `bnc_tab` which contains the frequencies of 950,000 word types of, say, the British National Corpus, and you can just type `bnc_tab["fish"]` or `bnc_tab[c("fish", "silk")]` to look up the frequency of that word / those words in the corpus.

### Exercises 3

Use R functions to

1: find in which position of the vector `qwe <- c(1,3,5,4,2,8)` the 5 occurs the first time:

```{r exercises3_1}
qwe <- c(1,3,5,4,2,8)
which(qwe==5)[1]
match(5, qwe)
```

2: find out which of the numbers from 1 to 10 are in `qwe`:

```{r exercises3_2}
(1:10)[1:10 %in% qwe]
intersect(1:10, qwe)
```

3: find out which of the numbers from 1 to 10 are not in `qwe`:

```{r exercises3_3}
(1:10)[!(1:10 %in% qwe)]
setdiff(1:10, qwe)
```

## Loading & outputting vectors

A very powerful function to load vector data into R is the function `scan`. This function can take many different arguments but here are some of its most important ones:

* the first argument is called `file` and is a single-element character vector specifying the location of the file you want to load;
* the second one is called `what` and is a single-element character vector defining what you are trying to load:
   + if you want to load a file that contains numbers, you need not specify that because that's `scan`'s default expectation;
   + if you want to load a file that contains character strings, specify `what=character()` or `what=""`;
* the last one mentioned here is called `sep` and is a single-element character vector providing the character that separates different entries (e.g., numbers or character strings) in the input file; often, this is set to
   + `sep="\n"`, if every number/character string has its own line;
   + `sep="\t"`, if the numbers/character strings are tab-delimited;
   + `sep=","`, if the numbers/character strings are comma-separated;
   + `sep=" "`, if the numbers/character strings are separated by spaces (the default).

There's a variety of other settings that can be useful and that I encourage you to explore once you have a bit more experience or feel you need them (especially `quote`, `fileEncoding`, and `encoding`). To load a file [_inputfiles/02_vector1.txt](_inputfiles/02_vector1.txt), which contains what is shown here, ...

```
1
2
3
```

into a vector `x`, we would therefore enter this:

```{r scannum}
(x <- scan(          # make x the result of scanning
   file="_inputfiles/02_vector1.txt",    # this file
   sep="\n")) # w/ elements separated by line breaks
```

To load a file [_inputfiles/02_vector2.txt](_inputfiles/02_vector2.txt), which looks like this, into a vector `x`, ...

```
This is the first line
This is the second line
```

... we would probably want to enter this:

```{r scanchar}
(x <- scan(          # make x the result of scanning
   file="_inputfiles/02_vector2.txt", # this file
   what=character(), # which contains character strings (also possible: what="")
   sep="\n"))        # w/ elements separated by line breaks
```

Now, how do we save vectors into files? The required function -- basically the reverse of `scan` -- is `cat` and it takes very similar arguments:

* the first argument is the name of the vector to be saved;
* the second one is called `file` and is a single-element character vector specifying the location of the file into which you want to save the vector (if no `file` argument is provided, output is printed to the console);
* the third one is called `sep` and defines the character that separates the elements of the vector from each other just as in `scan`;
* the final argument I mention here is called `append` and is a single-element logical vector:
   + if `append=FALSE` (the default), then `cat` will save the vector in the specified file but if that target file already exists, then it'll be overwritten;
   + if `append=TRUE`, then `cat` will save the vector in the specified file but if that target file already exists, then the new output will be appended to what's already in that file.

# Some other file operations

We already discussed loading/saving vectors with `scan`/`cat` so here will only discuss three other functions we will use in the remainder of this course. First, this is how we see which files and folders are in our [current working directory](https://en.wikipedia.org/wiki/Working_directory):

```{r dir1}
#| eval: false
dir() # no results shown -- try it yourself
```

And how do you know what your current working directory is? From `getwd`:

```{r getwd}
#| eval: false
getwd() # no results shown -- try it yourself
```

If you want the 'complete/normalized' path, you can add `normalizePath`:

```{r normalizePath}
#| eval: false
normalizePath(dir()) # no results shown -- try it yourself
```

But `dir` can take more arguments:

```{r dir2}
dir(                   # show the content
   path="_inputfiles", # of this folder
   all.files=TRUE,     # all files (incl. hidden ones)
   include.dirs=TRUE,  # include names of sub-directories
   full.names=TRUE,    # return the full paths (from the working directory)
   recursive=TRUE)     # include all sub-directories
```

To unzip a complete zip file, you can use `unzip`:

```{r unzip}
dir("_inputfiles", include.dirs=TRUE) # show what's in _inputfiles/
unzip(                               # unzip
   "_inputfiles/brown_untagged.zip", # this zip file
   exdir="_inputfiles")              # into this directory
dir("_inputfiles", include.dirs=TRUE) # show what's in _inputfiles/ now
```

To delete files or folders, you can use `unlink`:

```{r unlink}
unlink("_inputfiles/brown_untagged", recursive=TRUE)
dir("_inputfiles", include.dirs=TRUE) # show what's in _inputfiles/ now
```

# Programming constructs 1: loops

This section introduces loops, which are one useful way to execute one or more functions several times. While R offers several different ways to use loops, I will only introduce one of them here in a bit more detail, namely `for`-loops (others are `while` and `repeat` loops). A `for`-loop has the following structure:

```{r forloops}
#| eval: false
for (some.name in some.sequence) {
   what to do as often as some.sequence has elements
   this can be more than one line
}
```

The expression `some.name` stands for any name you choose to assign, and `some.sequence` stands for anything that can be interpreted as a sequence of values (where one value is actually enough to constitute a sequence); in this course, such a sequence will nearly always be a vector, namely a vector with integers from 1 to `length(some.sequence)` (either generated using the range operator `:` or `seq`). The values of that sequence are then usually used to access a part/subset of some data structure one at a time and do something with it. Let's first look at a very simple example where we

* define a vector `corp.files` containing a few file paths, for which we use `unzip` and then `dir`;
* load each one of those separately with `scan`;
* print out the third element with `cat` (because it will tell us whether the file contains spoken data (if it contains "stext") or not.

```{r combiningthings}
unzip(
   "_inputfiles/bncsgml_sample.zip",
   exdir="_inputfiles")
corp_files <- dir("_inputfiles/bncsgml_sample", full.names=TRUE)

for (i in seq(corp_files)) {     # for each of the files, create a number i
   curr_corp_file <- scan(       # make curr_corp_file the result of loading
      corp_files[i],             # the i-th of corp_files
      what=character(),          # which contains a character vector
      sep="\n",  quiet=TRUE,     # separated w/ line breaks, suppress message
      quote="", comment.char="") # don't process quotes & comment characters
   cat("File", i, ":\t", curr_corp_file[3], "\n")  # print the 3rd line, followed by a line break
}

unlink("_inputfiles/bncsgml_sample", recursive=TRUE) # housekeeping
```

# Session info

It's probably a good idea to end every script with this, to have an easy automatic record of all sorts of things you used during the session:

```{r}
sessionInfo()
```
