Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to R.

Similar presentations


Presentation on theme: "Introduction to R."— Presentation transcript:

1 Introduction to R

2 R Suite of software facilities for data manipulation, calculation and graphical display. Effective data handling and storage facility, A suite of operators for calculations on arrays, large, coherent, integrated collection of intermediate tools for data analysis Graphical facilities for data analysis and display Simple and effective programming language (called ‘S’)

3 R Environment within which many classical and modern statistical techniques have been implemented. A few of these are built into the base R environment, but many are supplied as packages . To do geostatistical analysis and mapping, many recognize R+gstat as the only complete and fully-operational package.

4 Variable Assignment We assign values to variables with the assignment operator "=". Just typing the variable by itself at the prompt will print out the value. We should note that another form of assignment operator "<-" is also in use. > x = 1  > x  [1] 1

5 Functions R functions are invoked by its name, then followed by the parenthesis, and zero or more arguments. The following apply the function c to combine three numeric values into a vector. > c(1, 2, 3)  [1] 1 2 3 TC_g.kg = Min. 1st Qu. Median Mean 3rd Qu. Max. Log(TC_g.kg) = Min st Qu. Median Mean 3rd Qu. Max.

6 Comments All text after the pound sign "#" within the same line is considered a comment. > # this is a comment [1] 2 Q1-Q3 = encompass 50% of all the data (this is 50% around the median – which remember is the midpoint of the data). The central 50% is not influenced by any of the extreme values so will give you a better estimate of the variability you may expect. TC_g.kg = Min. 1st Qu. Median Mean 3rd Qu. Max. Log(TC_g.kg) = Min st Qu. Median Mean 3rd Qu. Max.

7 Basic data Types Numeric Integer Complex Logical Character

8 Data Types: Numeric Decimal values are called numerics in R.
It is the default computational data type. If we assign a decimal value to a variable x as follows: > x = 10.5       # assign a decimal value  > x              # print the value of x  [1] 10.5  > class(x)       # print the class name of x  [1] "numeric"

9 Data Types: Numeric Furthermore, even if we assign an integer to a variable k, it is still being saved as a numeric value. > k = 1  > k              # print the value of k  [1] 1  > class(k)       # print the class name of k  [1] "numeric"

10 Data Type: Integer In order to create an integer variable in R, we invoke the as.integer function. We can be assured that y is indeed an integer by applying the is.integer function. > y = as.integer(3)  > y              # print the value of y  [1] 3  > class(y)       # print the class name of y  [1] "integer"  > is.integer(y)  # is y an integer?  [1] TRUE

11 Data Type: Integer Incidentally, we can coerce a numeric value into an integer with the same as.integer function. > as.integer(3.14)    # coerce a numeric value  [1] 3

12 Data Types: Logical A logical value is often created via comparison between variables. > x = 1; y = 2   # sample values  > z = x > y      # is x larger than y?  > z              # print the logical value  [1] FALSE  > class(z)       # print the class name of z  [1] "logical" Transformations may include: Log, square root, power, arc-sine, box-cox (this runs through the variety of available transformations and allows you to select).

13 Data Type:Character A character object is used to represent string values in R. We convert objects into character values with the as.character() function: > x = as.character(3.14)  > x              # print the character string  [1] "3.14"  > class(x)       # print the class name of x  [1] "character" Two character values can be concatenated with the paste function: > fname = "Joe"; lname ="Smith"  > paste(fname, lname)  [1] "Joe Smith"

14 Vectors A vector is a sequence of data elements of the same basic type. Members in a vector are officially called components or members. Here is a vector containing three numeric values 2, 3 and 5. > c(2, 3, 5) [1] And here is a vector of logical values. > c(TRUE, FALSE, TRUE, FALSE, FALSE) [1] TRUE FALSE TRUE FALSE FALSE A vector can contain character strings. > c("aa", "bb", "cc", "dd", "ee") [1] "aa" "bb" "cc" "dd" "ee" Incidentally, the number of members in a vector is given by the length function. > length(c("aa", "bb", "cc", "dd", "ee")) [1] 5

15 Vectors Extract a vector from a matrix
obs=values[,4] # store last column in a variable

16 Input/Output Read data from a file Write data on a file
setwd("C:\\Users\\morti\\Desktop") # set working directory values=read.csv("random.txt", header = TRUE, sep = "") # read from txt file Write data on a file write("RESULTS", file="data.txt")

17 Statistical Descriptors
Calculate the statistics of this file: mean, median, standard deviation, variance, quartiles (1 and 3) mean(a) median(a) sd(a) var(a) quantile(a,prob=seq(0,1,0.25))

18 Statistical Descriptors
obs=values[,4] # store last column in a variable print (obs) # set working directory mean_obs= mean(obs) # calculate mean of the observations median_obs=median(obs) # calculate the median of the observations variance=var(obs) # calculate variance st_dev=sqrt(var) # calculate standard deviation range_obs= range(obs) # calculate range range_res=abs(max(obs)-min(obs)) # calculate range q25=quantile(obs, c(.25)) # calculate quantile 25

19 Normal Distribution in R
pnorm(84, mean=72, sd=15.2, lower.tail=TRUE)


Download ppt "Introduction to R."

Similar presentations


Ads by Google