R Data Structures


  R Interview Questions

There are 4 basic data structures in R. I am not referring to the basic data types (numeric, integer, character, logical, complex ).

1. Vector – Sequence of elements of the same basic data type.

# These are some examples of vectors. 
# A numeric vector showing temperatures in Chicago of a particular week.
> temp = c(12.4, 13.5, 15.6, 20, 21.5, 13.6, 12.4)
> temp
[1] 12.4 13.5 15.6 20.0 21.5 13.6 12.4
# You cannot have different data types in a vector. 
# Since 12.4 is now defined as a string, all of the numbers have been
# converted to strings automatically.
> temp = c(12.4, 13.5, 15.6, 20, 21.5, 13.6, "12.4")
> temp
[1] "12.4" "13.5" "15.6" "20"   "21.5" "13.6" "12.4"

2. Matrix – If a Vector is 1D (1-dimensional), a matrix is 2D

# For example, if the vector temp_month contains the temperatures in 
# Chicago for an entire month
> temp_month
 [1] 12.4 13.5 15.6 20.0 21.5 13.6 12.4 13.2 12.6 17.6
[11] 12.8 19.4 14.3 16.7 17.2 14.5 16.7 19.2 21.5 14.0
[21] 19.4 19.3 17.5 18.5 20.1 22.5 23.5 34.1
<pre class="wp-block-syntaxhighlighter-code"># this can be folded into a 4x7 matrix as below
&gt; month_matrix = matrix(temp_month, nrow=4, ncol=7, byrow=TRUE)<div>&gt; month_matrix</div><div>     [,1] [,2] [,3] [,4] [,5] [,6] [,7]
[1,] 12.4 13.5 15.6 20.0 21.5 13.6 12.4
[2,] 13.2 12.6 17.6 12.8 19.4 14.3 16.7
[3,] 17.2 14.5 16.7 19.2 21.5 14.0 19.4
[4,] 19.3 17.5 18.5 20.1 22.5 23.5 34.1</div></pre>

3. List – Like a vector, but not limited to “same data type”

# For example, different data types like a person's age (numeric), 
# name(string), city(string),zip code (numeric) can be stored in a list. 
&gt; person = list(30, "Ajay", "San Francisco", 94000 )
&gt; person
[[1]]
[1] 30
[[2]]
[1] "Ajay"
[[3]]
[1] "San Francisco"
[[4]]
[1] 94000

4. Data Frame – Like a Matrix, but not limited to “same data type”

<pre class="wp-block-syntaxhighlighter-code"># A data frame is usually created by reading from external data
# using functions like read.csv() or read.table().
# Imagine 4 vectors
&gt; age = c(30,32,21,60)<div>&gt; names = c("Ajay","Adam","Mary","Aishu")
&gt; cities = c("San Francisco","New York","Sunnyvale","San Jose")
&gt; zip = c(94000,40101,94010,94001)

</pre>
<pre class="wp-block-syntaxhighlighter-code"># We can combine the vectors into a data frame like so
&gt; persons = data.frame(age,names,cities,zip)<div>&gt; persons</div><div>  age names        cities   zip
1  30  Ajay San Francisco 94000
2  32  Adam      New York 40100
3  21  Mary     Sunnyvale 94010
4  60 Aishu      San Jose 94001</div></div>
</pre>

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: