• Home
  • University of Queensland
  • Analysis Of Scientific Data
  • Introduction to R Programming for Data Analysis

Introduction to R Programming for Data Analysis

R Shortcuts Addition > 2+2 [1] 4 Multiplication 2^32 [1] 4294967296 Storing a variable myvalue = 42 > myvalue*myvalue [1] 1764 Entering small lists heights = c (174, 160, 169, 166, 182, 153, 191, 162) Eg. getting a length of a list length (heights) [1] 8 Mean > mean (heights) [1] 169.625 Median > median (heights) [1] 167.5 Standard Deviation > sd (heights) [1] 12.36282 Interquartile Range > IQR (heights) [1] 14.5 Log function > log (100, base=10) [1] 2 To read code into R: Name of data = read. csv (" . CSV") · str (quiz2) gives the structure of the data ·nrow (quiz2) gives the number of rows in the data · names (quiz2) gives just the variable names . head (quiz2) gives the first 6 rows of the data Use library (lattice) to be able to see graphs in R. To make a histogram: histogram (~ Variable, data=name) histogram (~ Height, data=quiz2, nint=20) (twenty bars) . To make a BW plot: bwplot (~ Height, data=quiz2) bwplot (Height ~ Superpower, data=quiz2) > Several BW boxes of superpower categories. To make a scatter plot: xyplot (Mass ~ Height, data=quiz2) xyplot (Mass ~ Height, data=quiz2, groups=Superpower, auto. key=TRUE) > colour coded scatter plot with legend Mean of all data: mean (quiz2$Height) Table of values: table (quiz2$Eyes) Summary of all data: summary (quiz2) Aggregate: The aggregate function in R is useful for calculating statistics within each group. For example, if you called your data frame coins then aggregate (Heads ~ Coin, data=coins, mean) will calculate the mean number of heads separately for each coin denomination used. aggregate(LogAddedSugar ~ Sex, data=floss, mean) If a random variable X has a Binomial (n, p) distribution, then the probability P (X=x) can be calculated in R using dbinom (x, n, p) > eg. dbinom (12,25, 0.5) (in the context of tossing coins) > the probability of getting 12 out of 25 with a probability of 0.5. T.test: t. test (islands$Change, alternative="greater") > Note that taking T15 - T0 means that we would expect a positive value for Change if the alternative hypothesis is true. Thus we use alternative="greater" in our test. The t statistic to test for a difference in mean initial pulse rate between males and females is: t.test(T0 ~ Sex, data=caffeine) Creating a variable out of the variables: caffeine$Change = caffeine$T15 - caffeine$T0 Pearson correlation: cor (data$Height, data$Attractive) Replicate function: Replicate (100, ) True or false ( > or < (maybe including an =) 0.1145) Sum > sum ( Binomial coefficient > dbinom (x=6, size=10, p=0.3) . pnorm (0.52) qnorm (.99) qt (0.975, df=10-1) t.test (data$Variable) t. test (data$Variable, alternative = "greater") rnorm (n= , mean = , sd = ) (basically generates random normal distribution values . linear model: lm(Mass ~ Height, data=) we want to estimate the average basal oxytocin level, uy, of all women who are x *= 40 years old. predict (oxy.age, newdata=data. frame (Age=40) ) predict (oxy.age, newdata=data. frame (Age=40) , interval="confidence") . prop. test (table (psp$Gender, psp$Response)