Wikipedia states that R is "multi-paradigm: array, object-oriented, imperative, functional, procedural, reflective and also dynamic. But what about its type safety? Please explain the different aspects/kinds of possible type safety in R with examples, e.g.
These questions are derived from Wikipedia (http://en.wikipedia.org/wiki/Strong_typing) as strong/weakly typed is too fuzzy to ask for (thanks delnan for clarifications here).
R is an open source programming language and software environment for statistical computing and graphics. It is one of the primary languages used by data scientists and statisticians alike. It is supported by the R Foundation for Statistical Computing and a large community of open source developers.
A type-safe language is one where the only operations that one can execute on data are the ones that are condoned by the data's type. That is, if your data is of type X and X doesn't support operation y , then the language will not allow you to to execute y(X) .
Since R language is weakly typed language, or dynamically typed similar to Perl, Python or Matlab/Octave, most of R users omit to place type checks in their functions if not rarely.
From wikipedia: Type safety: Type safety is synonymous with one of the many definitions of strong typing; but type safety and dynamic typing are mutually compatible.
> a = TRUE
> a
[1] TRUE
> a[3] = 1
> a
[1] 1 NA 1
> a[5] = 3.14
> a
[1] 1.00 NA 1.00 NA 3.14
> a = "Ala"
> a
[1] "Ala"
You can see it's dynamic - I can assign anything to variable a
and change it at will. It's also weakly typed performing implicit conversions liberally - see how I can add ints and then floats to what was originally boolean "vector". You can usually treat booleans as is they were numbers and numbers as if they were strings.
Implicit coercion by operators and functions is widespread in the language (and it has argument recycling). Warnings are issued most of the time for the recycling but not for coercion.
a <- c(TRUE, FALSE)
b <- c(-4:4)
a < b
#[1] FALSE FALSE FALSE FALSE FALSE TRUE TRUE TRUE TRUE
#Warning message:
#In a < b : longer object length is not a multiple of shorter object length
c(a,b)
# [1] 1 0 -4 -3 -2 -1 0 1 2 3 4
a + b
[1] -3 -3 -1 -1 1 1 3 3 5
# Standard Warning message re: multiple of shorter object length
> data.frame( a =a, b=b)
Error in data.frame(a = a, b = b) :
arguments imply differing number of rows: 2, 9
But if arguments to data.frame
have lengths that are an even multiple of the vector with the longest lenght, they get recycled without warning:
> data.frame( a =c(TRUE,FALSE,TRUE), b=b)
a b
1 TRUE -4
2 FALSE -3
3 TRUE -2
4 TRUE -1
5 FALSE 0
6 TRUE 1
7 TRUE 2
8 FALSE 3
9 TRUE 4
Functions are made polymorphic via two class systems, one of which depends on the period character signalling the target class, the other of which depends on object class signatures. (And the function naming conventions are very erratic.)
I disagree with the premise of R as an object-oriented language. People with object-oriented experience that come to R expect a different behavior than it exhibits. It is a functional language that is dispatched with class systems that examine the objects given but it's not like a real object-oriented language.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With