 Introduction to Data Structures in R | - Asia B Technology

# Introduction to Data Structures in R |

R has a poly data structure. Here, introduce some that are not infrequently used only, for starters programming r before falling too much into using R. Danger! Vector

This data structure is the most poly data structure used in R. It can be said that vectors are the primary data structures in R. Vectors are a series of values claimed by elements, which have the same data type or mode. We can create vectors based on numeric elements, strings, or round numbers. The important thing is that all elements in one vector must be of the same type. Elements in this vector can be named. As we like, we know, what do we do.<-c value(statistics = 89, physics = 95, communication science = 100) #Menampilkan the contents of the value vector #Menampilkan the contents of the 2nd value #Mengambil and display the contents using the physical name of the value[“ecamatra”] > #Membuat named vector with the name of the value > the value <-c(statistics = 89, physics = 95, science communication = 100) > > #Menampilkan the content of the variable value > the statistical value of the communication science 8995100 > >#Menampilkan the second content based on the value vector > value[two] physics 95 > > #Mengambil and displays the content using the physical name > the value[“physics”] equivalent to 95Scalar

Scalar or individual values. This data structure can be said to be invalid to exist in R, because scalars (scalars) can be expressed as vectors with a length of 1.> x <- 6 > x  6

On x is given a scalar with a value of 6. Note the marker  in the output of the interactive console R.ini signifies R perceives x as a vector using an element of 1.String characters

The following example tells the string character data structure. This data structure is a vector or scalar with string mode.> x <- c(five,12,13) > x  five 12 13 > length(x)  three > modes(x)  “numeric” > y <- “abc” > y  “abc” > length(y)  1 > mode(y)  “character” > z <- c(“abc”, “29 88”) > length(z)  2 > mode(z)  “character”

The first example gives an x vector consisting of three elements with a numeric mode. Then vector y is a vector with a single string. What if a vector consists of string and numeric characters? R will change it as a character. Like the following model> x<-c(“abc”,1,3) > x  “abc” “1””3″ > xdanlt;-c(1,two,”abc”,1,3) > x  “1””2″”abc” “1””3″

R has a poly function for managing strings. Some deal with the merging of strings or their separation. Here’s an example.> u <-paste(“abc”,”de”,”f”) # merge strings > u “abc de f”> v <- strsplit(u,””) # separates strings from blank characters. > v[] “abc” “de” “f”[/code] Matrix

As the name implies, this data structure has the same meaning as the matrix in mathematics: that is, a number box arranged as an array. Basically a matrix is a vector, with additional attributes, that is, column numbers and row numbers. Here is a model according to the matrix.> m <- rbind(c(1,4),c(2,two)) > m [,1] [,2] [1,] 1 4 [2,] 2 2 > m % * % c(1,1) [,1] [1,] 5 [two,] 4

The first example uses the rbind function to combine two vectors as a matrix combined as rows. The cbind function can be used if you want to combine a vector into a column based on a matrix. The last example according to the example above is to do matrix multiplication between m using vectors (1,1). To access exclusive element values according to the matrix can be done with square brackets [a,b] to retrieve the values of the ath row and the bth column.Example> m[1,two]  4 > m[two,2]  two

In R, in addition to a single element, it can also be accessed to create columns or rows. Like the following model> m[1,] # row 1  1 4 > m[,2] # column 2  4 2List

Like vectors, a list is also a container for storing values. The difference is that on the list, list elements can be of any type, and not necessarily the same. Elements according to a list can be accessed using reaching into the list name & element name based on the list associated with the symbol “\$”.> x <- list(u=2, v=”abc”) > x \$u  2 \$v  “abc” > x\$u  2

The expression x\$u refers to the u component in list x. List x has two components, namely u & v.In general, lists are used to accommodate some of the results spewed out by a function. For example, in the hist() function, for example, we use a built-in data group based on R, namely Nile, and create a histogram of that data.data(Nile) #buat activate the Nile hist(Nile) data group #membuat histogram according to Nile xdanlt;-hist(Nile) data

What is x? It turns out that x is a list with components obtained based on the hist() function. Let’s see.In addition to producing output in the form of histogramsThe function also forms an x object in the form of a list that we can call where we are happy.> print(x) \$breaks 400500600700800900 1000 1100 1200 1300 1400 \$counts 105 20 25 19 12 1161 \$density  0.0001 0.0000 0.0005 0.0020 0.0025 0.0019 0.0012 0.0011 0.0006 0.0001 \$mids 450550650750850950 1050 1150 1250 1250 1350 \$xname  “Nile” \$equidist  TRUE attr(, “group”)  “histogram”

Ignore what the components mean. We are still at the early stage of acquaintance. There will also be lighting on other parts. The interesting thing is, that in addition to creating a histogram, the hist function () also stores other values components in the list form. In order for the appearance of this list to be more concise as a result easier to read, it can use the str command.> str(x) List of 6 \$ breaks: int [1:11] 400 500 600 700 800 900 1000 1100 1200 1300 … \$ counts: int [1:10] 1 0 5 20 25 19 12 11 6 1 \$ density : num [1:10] 0.0001 0 0.0005 0.002 0.0025 0.0019 0.0012 0.0011 0.0006 0.0001 \$ mids: num [1:10] 450 550 650 750 850 950 1050 1150 1250 1350 \$ xname: chr “Nile” \$ equidist: logi TRUE – attr(*, “group”)= chr “histogram” `strdanlt;/code> stands for structure. This function can be used to investigate the structure in each object in R. Let's add to the <h3dangt;f. data="" frame<="" h3=""> </h3dangt;f.>`

`Certain data groups have several types of data. For example, a group of data obtained according to the results of a survey of visitors based on a restaurant. The group can consist by name, gender, age, occupation, number of purchases. Because the data type is non-uniform, it cannot use matrix data structures. A data frame is a list, with each component in it as a column in a "matrix". We can create a frame data using the following method. > d<-data.frame(list(child=c("banu","dara"),age=c(13,11))) > str(d) 'data.frame': 2 obs. of2 variables: \$ child: Factor w / two levels "banu","dara": 1 two \$ age: num13 11 > print (d) children aged 1 banu13 two virgins > d\$age  13 11 > d[1, ] children aged 1 banu13`

The results of reading data groups according to files or databases, generally form a data frame. Classes Silahkan Subscribe untuk Mendapatkan Update Terbaru Sekitar Technology