# Introduction to R

R as a Tool for Statistical Analysis

Getting Started with R (Part I)

The > is called the prompt. If a command is too long to fit on a line, a + is used for the continuation prompt. Assign values to an object using the equal sign (=), e.g., >x=11. Note: R is case sensitive Print command allows you to see the value in an object

You may also use the less than sign and a dash to create an arrow (<-) to assign values. An equal sign (=) is also used to assign values. R easily overwrites objects, e.g. >x=9, 11 is overwritten) ls() command allows to see what is stored in R’s memory, we can look at the workspace in R to look at the memory rm command removes a an object from R’s memory, e.g., >rm(x) Objects in r may include numbers or periods, e.g., >x.1=14 We may also assign character values to R rather than numbers by including quotation marks around the characters, e.g., >xx=”Statistics”. Note: when numbers are in quotation marks, R will treat them as characters not numbers We may perform arithmetic operations in R, e.g., >11+14

We may also perform the same operations in objects in R, e.g. > x+x.1 We may wish to store this in a new object called z, i.e., >z=x.1+12 We can also perform other arithmetic operations, e.g. > 10^2 We can take the square root using the sqrt command, e.g., >sqrt(x), or >y^(1/2) We can take the natural logarithm using the log command, e.g. >log(y) We can take the exponent using the exp command, >exp(x)

We can also calculate log of other bases using the log base 2 command, e.g., >log2^(x) We can calculate the absolute value using the abs command, e.g. >abs(-14) Few helpful tips in using R:

If you enter an incomplete command, R will follow that up with a plus sign to let you know there’s an incomplete command, e.g., >sqrt(x the plus sign will appear then just add ) for R to return the answer Using the arrow up key in the key board will bring you to the last command entered in R, hitting the arrow up key again will bring you to the previous command To enter comments on the command, include # to tell R to ignore everything that follows, >#Behavioral Statistics Getting Started with R (Part II):

Creating vectors, matrices, and performing some simple operations We often want to create vectors of numbers and characters. We can create a vector in r using the concatenate (c) command, e.g. > x1=c(1,3,5,7,9) We can also create vectors of characters by including quotation marks around each character, e.g. >gender=c(“male”, “female”) We can create a sequence of integer values using colon, e.g., >2:7 For more general sequences, we can use seq command, e.g., >seq(from=1, to=7, by=1; also for non-integer values, e.g., >seq(from=1, to=7, by=1/3) or >seq(from=1, to=7, by=0.25) We can use the rep command to create a vector of repeated numbers or characters, e.g., rep(1, times=10) or >rep(“Statistics”, times=10) We may have a sequence repeated multiple times, e.g., >rep(1:3, times=5) We may want to repeat a sequence of numbers for which non-integer values are used, e.g. >rep(seq(from=2, to=5, by=0.25), times=5) We may also want a sequence of characters repeated multiple times, e.g., >rep(c(“m”, “f”), times=5) For x=1 2 3 4 5, we may add a value to each element of a vector using the +,-,*,/ command, e.g., >x+10 will return 10 11 12 1 3 14 15 If two vectors are of the same length, we may add/subtract/multiply/divide corresponding elements, e.g.,> x= 1 2 3 4 5; >y= 1 3 5 7 9; >x+y=2 5 8 11 14 Extracting elements

We may extract element in a vector using square brackets

if we want to extract the 3rd element in vector x= 1 2 3 4 5, the command is >x[3] if we want to extract all elements except the 3rd, we put the negative sign, e.g., >x[-3] To extract the first three elements, >x[1:3]

To extract the 1st and the 5th element, >x[c(1, 5)]

To extract all except the 1st and 5th elements,...

Please join StudyMode to read the full document