• Tidak ada hasil yang ditemukan

array (data, d

N/A
N/A
Protected

Academic year: 2025

Membagikan "array (data, d"

Copied!
8
0
0

Teks penuh

(1)

III. Array Array is a matrix with more than two dimensions A) Creating array

The syntax for creating an array is:

aaa

array (data, dim)rray (data, dim)rray (data, dim) rray (data, dim)

Example

> x=array (1:24, c (3, 4, 2))

> x , , 1

[,1] [,2] [,3] [,4]

[1,] 1 4 7 10 [2,] 2 5 8 11 [3,] 3 6 9 12 , , 2

[,1] [,2] [,3] [,4]

[1,] 13 16 19 22 [2,] 14 17 20 23 [3,] 15 18 21 24

B) Giving name to an array

same as matrices, two method of giving a name to an array can be used:

1- Use of rownames and colnames argument

> rownames(x)=c("row1","row2","row3")

> colnames(x)=c("col1","col2","col3","col4") NOTE:

There is no special function to naming the third dimension or more than third dimension such as rownames( ) for rows and colnames( ) for columns so we can use dimnames( ) function

(2)

> x , , first a b c d A 1 4 7 10 B 2 5 8 11 C 3 6 9 12 , , second a b c d A 13 16 19 22 B 14 17 20 23 C 15 18 21 24

Another method:

> x=array (1:24, c (3, 4, 2))

> dimnames(x)=list(NULL,NULL,c("first","scond"))

> rownames(x)=c("row1","row2","row3")

> colnames(x)=c("col1","col2","col3","col4")

> x , , first

col1 col2 col3 col4 row1 1 4 7 10 row2 2 5 8 11 row3 3 6 9 12 , , scond

col1 col2 col3 col4 row1 13 16 19 22 row2 14 17 20 23 row3 15 18 21 24

** Try:

array(1:24,c(2,2,3,2)) array(1:4,c(2,2,3))

(3)

C) Array indexing:

Using square brackets [first dimension, second dimension,…]

> x=array(1:24,c(3,4,2))

> x[1,4,1]

> x[,4,1]

> x[2,,2]

> x[2,2,]

> x[1,,]

> x[,,1]

Exercise:

Use of apply function on array

> array(1:4, c(2,2,3)) , , 1

[,1] [,2]

[1,] 1 3 [2,] 2 4 , , 2

[,1] [,2]

[1,] 1 3 [2,] 2 4 , , 3

[,1] [,2]

[1,] 1 3 [2,] 2 4

> apply( array(1:4, c(2,2,3)),2,sum) [1] 9 21

> apply( array(1:4, c(2,2,3)),1,sum) [1] 12 18

> apply( array(1:4, c(2,2,3)),3,sum) [1] 10 10 10

(4)

The following table contains information of 3 countries:

GDP Pop Inflation

Austria 197 8 1.8

France 1355 58 1.7

Germany 2075 81 1.8

Answer the following:

1. Create a matrix name it country.data

2. Find the total population for all three countries.

3. Add variable area (84,544,358)

4. Add country Switzerland (265, 7, 1.8,41) 5. Find the maximum value for all variables.

6. Find the mean for all variables.

7. Change the population of Austria to 10 millions 8. Give data for France.

9. Find population for Germany.

10.Define variable EU=c("EU","EU","EU","non-EU") and add it to the variables 11.Find the maximum value for all variables and see what the entries look like

(Error)????.

(5)

VI. Data frame

Are very similar to matrices except that they allow the columns to contain different types of data of the same length, whereas a matrix is restricted to one type of data only.

Data frames still have to be in rectangular form as matrices.

A) Creating data frame:

• read.table read data from external files.

• read.csv read data from external files.

• data.frame binds together objects of different kinds.

The syntax for creating a data frame is:

data.frame(data1,data2,…)

Example:

> data.frame(c(3,5,8),4:6,rev(6:8))

> data.frame(1:4,rep(1:2,2),seq(10,3,length=4)) B) Giving name to a data frames:

* If we just want to name the columns, like data vectors, data frames can have a column’s names by names( ) function or when we define the objects, (same as cbind( ) and rbind( ) )

* If we want naming the rows and columns, same as matrices, there are two methods:

the first one by using rownames( ) and colnames( ), the second by dimnames( ) at once.

Example:

> x=1:2

> y=letters[1:2]

> z=1:3

> data.frame(x,y) # appears with names

> data.frame(x,y,z)

3OTE:

Data frames must have variables of the same length.

Example:

(6)

C) Data frame Arithmetic:

You can only apply numeric computations to numeric variables in data frame.

D) Data frame Indexing:

Data frames can be accessed in several ways.

• Same tools used with matrix indexing can be used, df[row,col]

• And also we can use $ to extract vector (columns only).

• By data frame names, but first use attach( ) (columns only).

3OTE:

• When R attaches a data frame it makes a copy of the variables. If we make changes to the variables, the data frame is not actually changed.

• After using attach( ), it is better to use detach( ).

Example:

> d=data.frame(l=letters[4:8],b=c(T,T,F,F,T),n=7:3)

> d[1,2]

> d[4,3]

> d$l

> d$n

> d$b

> d$n[d$n>5]

> d[1,"n"]

> n # n is not there

> attach(d) # now l,b and n are there

> n

> n[3]

> n[3]=10

> n

> d # d not changed

> detach(d)

>n # n is there and changed

> l # not there

>d # d is not changed

(7)

Example:

Car information Example

car.inf=matrix(c(8895,33,2560,7402,33,2345,6319,37,1895),3,3,byrow=T) country=c("USA","Korea","Japan")

inf=c("Price","Mileage","Weight") dimnames(car.inf)=list(country,inf) dimnames(car.inf)

Compute the price of cars after 25% discount, then add this variable to car.inf data frame

dis=car.inf[,1]-car.inf[,1]*0.25 car.inf=cbind(car.inf,dis)

type=c("sporty","compact","van") car =cbind(car.inf,type)

car

car.frame =data.frame(car.inf,type) car.frame

car.frame$millage car.frame$ Mileage

car.frame["USA",] #Select info. of USA cars To add a row to data frame

SA=data.frame(7000,40,2000,4000,"van")

inf=c("Price","Mileage","Weight","dis","type") dimnames(SA)=list("SA",inf)

d1=rbind(SA,car.frame) d1

Price Mileage Weight dis type SA 7000 40 2000 4000.00 van USA 8895 33 2560 6671.25 sporty Korea 7402 33 2345 5551.50 compact Japan 6319 37 1895 4739.25 van

What happen if you use this

(8)

Access data.frame with logical vector

car.frame$Price[dis>5000]

car.frame$Price[Weight>2000] # Problem!!! Weight was not defined

# dis was defined separately so did not make a problem car.frame$Price[car.frame$Weight>2000]

car.frame["USA",]

car.frame$Mileage[car.frame[,1]>7000]

car.frame$Mileage[car.frame$Price>7000]

What is car.frame?????

car.frame[dis>4000,1:4]

car.frame[dis>5000,1:4]

subset:

To extract values from a data frame, returns subsets of vectors or data frames that meet specific requirements

new.frame=subset(car.frame,subset=dis>4000,select=1:4)

Referensi

Dokumen terkait

Elevated CO 2 increased lateral root length (20 months, trend at 4 months) and fine root length (4 months, trend at 8 and 20 months) of seedlings in the high-N treatment but not in

Array multidimensi adalah array yang memiliki banyak dimensi atau dengan kata lain array di dalam array dan jumlahnya tidak terbatas tergantung dari jumlah data yang akan dibuat.

This function is called with the names of the data frames, and in the simplest cases this is enough: it returns a merged data frame whose rows are defined by the values of the

Figure 10 Frames Received an Internet Gateway 3.2 QoS Measurement Considering the small size of each frame 23 bytes and the data rate of the IEEE 802.11 54 Mbps network, the major

Activity 1: Data for discussion 0 2 4 6 8 10 12 1995 1999 2003 2007 2011 2015 Percentage % Australian students not reaching Low benchmark in science Year 4 Science Year 8 Science

LIST OF FIGURES Figure 1 General Time Frame for the project 3 Figure 2 Sequence of images in a video 4 Figure 3 Example of current and reference frame 4 Figure4 Fotward Prediction

gray scale Pixel Color • To represent a gray-scale image of 4 colors for example we need to increase the length of bit- pattern representing the pixel to be 2 bits... colored pixel

Example: Iris data § We show how the attributes, petal length, petal width, and species type can be converted to a multidimensional array – First, we discretized the petal width and