## OT: R limbo 101 [Software]

» I thought something like if... else... would be very logical and easy. Maybe because these are real words from a language I can understand.

Perfectly understandable. Of course, these constructs exist in R and we use them all the time. The point is only that direct access of values (with a condition) is simply more

*efficient*.

If you don’t have to deal with large data sets, both are fine. Since

*I*deal a lot with simulations (1 mio BE studies with a sometimes high number of subjects / periods / sequences, …) I learned the hard way to avoid the ‘simple’ constructs if ever possible (given, sometimes I know that it could be done but was too stupid).

Think about my previous example. For 1 mio calls the direct access is 15times faster than the loop and

`if()`

within. It’s not unusual to have *nested*calls. For two it would by ~230times slower and for three already ~3,500times. Would test your patience.

» But it looks like R and I are following different kinds of logics.

Not necessarily so. See above.

» When you learn a new language and start speaking it with somebody, that person will usually show some goodwill, ignore grammatical mistakes and try and understand what you mean. R made no efforts of any kind, even though what I was trying to achieve was pretty obvious

I agree. See the subject line. The man-pages of ’s functions were written by statisticians, regularly forgetting non-expert users. Sometimes the response you get is bizarre. Want to know how

`for()`

works?`?for`

+

`+`

Fuck, get me out of here!

`help(for)`

Error: unexpected ')' in "help(for)"

What the heck? Shall I really try it *without* the closing bracket? Strange. OK, OK.

`help(for`

+

Aha, the closing bracket *is* missing!

`) `

`Error: unexpected ')' in:`

"

)"

You can’t be serious! Google, google, reading the R-Inferno Section 8.2.30. Oh my!

`?`

**`**for**`**

help(**`**for**`**)

Both work as expected. I don’t know what’s the logic behind. Beyond me.

» » If you have a vector of data (say

`y`

)»

» Yeah, ElMaestro started using this kind of vocabulary too when we were exchanging email. […] All I learnt at school about vectors is the geometric side of the concept - what Wikipedia apparently calls Euclidian vector.

Correct.

» I had some problems understanding what an arrow between two points had to do with what I was trying to achieve.

Absolutely nothing, of course. In a one-dimensional array is meant. The indexing (

*i.e.*, where it starts) differs between languages.

**0**

C, C++, BASIC-dialects, Pascal (default; can be declared as 1), PHP, Perl, Java, JavaScript, Lisp, Python, Ruby, Scheme, …

**1**

S, R, Julia, FORTRAN, ALGOL, AWK, COBOL, Matlab, Mathematica, Wolfram Language, XQuery, …

*scalar*(a single number, character, string, boolean),

*one-*(R: vector) and

*multidimensional array*(R: data.frame and matrix). In R there are no scalars, only vectors containing a single element instead.

`x1 <- 1`

x2 <- 1L

x3 <- TRUE

x4 <- NA

x5 <- "a"

comp <- matrix(data = c(c(x1, x2, x3, x4, x5),

c(is.vector(x1), is.vector(x2), is.vector(x3),

is.vector(x4), is.vector(x5)),

c(length(x1), length(x2), length(x3),

length(x4), length(x5)),

c(typeof(x1), typeof(x2), typeof(x3),

typeof(x4),typeof(x5))),

nrow = 5, ncol = 4,

dimnames = list(paste0("x", 1:5),

c("value", "vector?", "length", "type")))

print(as.data.frame(comp))

value vector? length type

x1 1 TRUE 1 double

x2 1 TRUE 1 integer

x3 TRUE TRUE 1 logical

x4 <NA> TRUE 1 logical

x5 a TRUE 1 character

What makes so powerful is yet another data type, namely the

*list*. A list can contain any of the other types and a mixture of them (even other lists…).

```
char.vect <- letters[1:2]
```

int.vect <- as.integer(1:3)

mydf <- data.frame(x = 1:4, y = c(4:6, NA))

mymat <- matrix(data = c(100:102, 2*100:102), nrow = 3, ncol = 2,

dimnames = list(paste0("row", 1:3), c("x", "y")))

mylist <- list(char.vect = char.vect, int.vect = int.vect,

mydf = mydf, mymat = mymat)

print(mylist)

$char.vect

[1] "a" "b"

$int.vect

[1] 1 2 3

$mydf

x y

1 1 4

2 2 5

3 3 6

4 4 NA

$mymat

x y

row1 100 200

row2 101 202

row3 102 204

Elements can be accessed by their name or index.

```
mylist$mymat["row2", "y"]
```

[1] 202

mylist$mymat[2, 2]

[1] 202

mylist[[4]][2, 2]

[1] 202

The double square brackets are mandatory to access the root element of lists. Hence,

`mylist[4][2, 2]`

does not work:`Error in mylist[4][2, 2] : incorrect number of dimensions`

*Dif-tor heh smusma*🖖

Helmut Schütz

The quality of responses received is directly proportional to the quality of the question asked. 🚮

Science Quotes

### Complete thread:

- Spreadsheet failures, any recent examples? ElMaestro 2019-07-18 13:43 [Software]
- Spreadsheet failures, any recent examples? Ohlbe 2019-07-18 14:32
- Spreadsheet addiction Helmut 2019-07-18 16:12
- Nasty beast Ohlbe 2019-07-18 17:30
- Nasty beast Helmut 2019-07-18 20:26
- Nasty beast Ohlbe 2019-07-19 11:32
- Nasty beast ElMaestro 2019-07-19 12:32
- Decidedly off topic Ohlbe 2019-07-19 13:38

- OT: R limbo 101Helmut 2019-07-19 13:00

- Nasty beast ElMaestro 2019-07-19 12:32

- Nasty beast Ohlbe 2019-07-19 11:32

- Nasty beast Helmut 2019-07-18 20:26
- Spreadsheet addiction Shuanghe 2019-07-18 18:59
- Spreadsheet addiction Helmut 2019-07-18 20:04
- OT: Spreadsheet addiction Shuanghe 2019-07-19 12:41
- OT: Spreadsheet addiction Helmut 2019-07-19 14:29
- OT: Spreadsheet addiction nobody 2019-07-19 15:53
- OT: Spreadsheet addiction Helmut 2019-07-19 19:32

- OT: Spreadsheet addiction nobody 2019-07-19 15:53

- OT: Spreadsheet addiction Helmut 2019-07-19 14:29

- OT: Spreadsheet addiction Shuanghe 2019-07-19 12:41

- Spreadsheet addiction Helmut 2019-07-18 20:04

- Nasty beast Ohlbe 2019-07-18 17:30
- Spreadsheet etc. failures zizou 2019-07-20 00:18
- As designed ☺ Helmut 2019-07-20 02:12
- As designed ☺ ElMaestro 2019-07-20 09:08
- To round or not to round… Helmut 2019-07-20 12:53
- To round or not to round… ElMaestro 2019-07-20 19:59
- floating-point math is always more complex than you think it is mittyri 2019-07-20 22:43

- To round or not to round… ElMaestro 2019-07-20 19:59

- To round or not to round… Helmut 2019-07-20 12:53

- As designed ☺ ElMaestro 2019-07-20 09:08

- As designed ☺ Helmut 2019-07-20 02:12

- Spreadsheet addiction Helmut 2019-07-18 16:12
- Spreadsheet failures, any recent examples? Ohlbe 2020-12-10 18:43
- Floating point arithmetic, again Helmut 2020-12-10 19:12
- Floating point arithmetic, again ElMaestro 2020-12-10 19:40
- Floating point arithmetic, again Ohlbe 2020-12-10 20:18
- Floating point arithmetic, again ElMaestro 2020-12-10 21:38
- Floating point arithmetic, again Ohlbe 2020-12-10 21:46
- Floating point arithmetic, again ElMaestro 2020-12-10 22:05

- Floating point arithmetic, again Ohlbe 2020-12-10 21:46

- Floating point arithmetic, again ElMaestro 2020-12-10 21:38

- Floating point arithmetic, again Ohlbe 2020-12-10 20:18
- Floating point arithmetic, again Ohlbe 2020-12-10 20:13

- Floating point arithmetic, again ElMaestro 2020-12-10 19:40
- From bad to worse Ohlbe 2020-12-10 22:11
- From bad to worse mittyri 2020-12-11 00:22
- All is good Helmut 2020-12-11 00:36
- Float is float PharmCat 2020-12-18 20:53
- Float is float! Helmut 2020-12-20 23:27
- rational solution in R mittyri 2020-12-21 13:49
- related stuff Helmut 2021-01-14 12:53

- rational solution in R mittyri 2020-12-21 13:49

- Float is float! Helmut 2020-12-20 23:27

- Float is float PharmCat 2020-12-18 20:53

- Floating point arithmetic, again Helmut 2020-12-10 19:12

- Spreadsheet failures, any recent examples? Ohlbe 2019-07-18 14:32