Use R in C#: R data types

Maybe you want to use R in C# to do some basic data analysis and create some nice plots. In such cases it is not necessary to learn the whole R syntax and know all R features. But it will be helpful to have some basic R knowledge. Within this article I want to give a short overview about the type system in R.

 
It’s all about vectors

The vector is the most important data type in R. It represents an array of a single kind of value. Most other data types are based on vectors. Even a single value is a vector with just one element.

The following source code will show some basic vector operations. The hash sign (#) is used in R for single line comments. A hash anywhere on a line will comment out the rest of the line.

foo <- 3                                               #creates a vector with a single element

foo <- c(1,2,3)                   #creates a vector with three elements („c“ is the concatenate function)

 

To access the vector elements you may use the indexing operator “[index]”. The index itself is also a vector. The indexing operator returns the elements of foo contained in that index vector.

foo <- c(6,7,8)

foo[1]                   #returns 6

foo[c(1,2)]          #returns a vector containing 6 and 7

foo[c(2,3,3,1)] #returns a vector containing 7,8,8,6

 

You can also assign “tags” or “names” to the elements of the vector:

foo <- c(first=1, second=2, third=3)

 

To access named elements you will use the “$” operator.

foo <- c(first=1, second=2, third=3)

foo$second       #returns 2

 
List

As mentioned above, a vector can only contain elements of a single kind of value. Therefore it is called “atomic vector”. But of course, you will often have different kind of values. In this case you need to use a list.

A list is also a vector which is called a “generic vector” where each element can hold an atmomic vector or another list.

foo <- list(1, 2.0, „three“)            #creates a list (generic vector) with three vectors of one element each

foo <- list( c(1,2,3), 2.0, „three“)              #creates a list with three vectors where the first vector contains three elements

 
To access the vector elements you may also use the indexing operator. As the structure of a list is different to the one of atomic vectors, another indexing mechanism exists, which is written with two brackets: “[[index]]”. In this case index must be a single element integer vector and the indexing operator returns the elements of foo at this index.

foo <- list(1, 2.0, „three“)

foo[[1]]               #returns 1

foo[[3]]               #will return „three“

 
You may also use the single bracket index operation for lists. As you will access several elements of different kinds the return value must also be a list.

foo <- list(1, 2.0, „three“)

foo[1]                   #returns a list which contains the vector 1

foo[c(1,3)]          #returns a list with two vectors

 
Like in atomic vectors you can also assign “tags” or “names” to the elements of the list and access them with the “$” operator.

foo <- list(first=1, second=2.0, third=“three“).

foo$second       #returns 2.0

 
Attributes

Almost every data type in R can have attributes. You can get and set them by using the „attr“ function.

foo <- c(1,2,3)

attr(foo, „bar“) <- “baz”                               #adds the attribute “bar”

attr(foo, „bar“)                                #get the attribute “bar”

 
To get the list of all attributes for an object “foo”, you can use “attributes(foo)”.

 
Matrices

Matrices can be created using the „matrix“ function. You have to pass the data you want within your matrix, in column-major order. And you have to set the number of rows or the number of columns in the matrix (or both).

foo <- matrix(c(1,2,3,4), nrow=2, ncol=2)

This creates a matrix with 2 rows and 2 columns. The value vector is interpreted in column-major order. So the first column contains the values 1 and 2 and the second column contains the values 3 and 4. As you can see, the matrix itself is nothing but a vector with two additional attributes.

You can access single elements, rows and columns of the matrix using „[]“

foo[2,1]               #returns the an element in row 2 and column 1

foo[3,]                 #returns the third row

foo[,2]                 #returns the second column

 
Summary

This article gave you a short summary about the most important R types. Of course, there are several other types, but they all are based on vectors. So with the knowledge of this article you can implement you first R applications.

Werbung
Dieser Beitrag wurde unter .NET, C#, R veröffentlicht. Setze ein Lesezeichen auf den Permalink.

Kommentar verfassen

Trage deine Daten unten ein oder klicke ein Icon um dich einzuloggen:

WordPress.com-Logo

Du kommentierst mit deinem WordPress.com-Konto. Abmelden /  Ändern )

Facebook-Foto

Du kommentierst mit deinem Facebook-Konto. Abmelden /  Ändern )

Verbinde mit %s