Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Statistics Study Notes: Generating and Manipulating Data in R, Assignments of Statistics

R code examples and explanations for generating and manipulating data in r, including creating vectors, generating equidistant series, using sequential functions, replicating elements, and generating random samples. It also covers arithmetic operations, visualizing distributions, and calculating relative frequencies.

Typology: Assignments

Pre 2010

Uploaded on 09/02/2009

koofers-user-6nx-1
koofers-user-6nx-1 🇺🇸

3.7

(3)

10 documents

1 / 4

Toggle sidebar

Related documents


Partial preview of the text

Download Statistics Study Notes: Generating and Manipulating Data in R and more Assignments Statistics in PDF only on Docsity! STAT 324 Discussion #2 TA Jiale Xu Office 1245D MSC Phone 262-5767 Email xujiale@stat.wisc.edu Office hour Tue. 2:10-3:20pm and Wed. 2:10-3:00pm 1 Generating Data 1.1 Combining Values into a Vector or List c(...) is used to define vectors. > height <- c(1.8, 1.90, 1.74, 1.75) > height [1] 1.80 1.90 1.74 1.75 1.2 Generating Equidistant series of numbers seq() generates regular sequences. > seq(3, 9) [1] 3 4 5 6 7 8 9 > seq(3, 9, by = 2) [1] 3 5 7 9 1.3 Sequential Function ”:” seq(1:9) can also be written using a special syntax > 3:9 [1] 3 4 5 6 7 8 9 1.4 Replicating Elements of Vectors rep() can generate repeated values. > rep(1:4, 2) [1] 1 2 3 4 1 2 3 4 > rep(1:4, c(2,2,2,2)) [1] 1 1 2 2 3 3 4 4 1 1.5 Generating Random Samples sample() takes a sample of the specified size from the elements of x using either with or without replacement. The general form is: sample(x, size, replace = FALSE, prob = NULL) > sample(1:10,8) [1] 5 4 9 1 2 8 10 6 By default size is equal to length(x) so that sample(x) generates a random permutation of the elements of x (or 1:x). > sample(1:10) [1] 6 1 3 2 5 7 8 10 4 9 Note that by default the sampling is without replacement. To generate sample with replacement, we need to add the argument ”replace = TRUE”. > sample(1:10,20,replace = TRUE) [1] 3 6 4 1 3 2 2 3 1 8 5 3 2 2 10 5 7 2 5 9 Example: > die1 <- sample(1:6, 100, replace = TRUE) > xtabs(~ die1) die1 1 2 3 4 5 6 21 20 16 14 16 13 > barchart(xtabs(~ die1)) 2 Operations on Vectors 2.1 Arithmetic Operations > x <- c(3, 8, 2) > y <- c(6, 7, 5) > 2*x [1] 6 16 4 > x^2 [1] 9 64 4 > x+y [1] 9 15 7 > x*y [1] 18 56 10 > x/y [1] 0.500000 1.142857 0.400000 > sum(x) 2
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved