Professional Documents
Culture Documents
1, 2, 3,
a:b 1:5
4, 5
7, 7, 8,
rep(x, times, each, rep(c(7, 8), times = 2, each
8, 7, 7,
length.out) = 2)
8, 8
Warning! Vectors contain either numbers or
characters, not both
sd(x), var(x),
sd(1:10) 3.03
range(x)
Returns a vector
unique(c(1, 1, 2,
unique(x) of all unique 1, 2, 10
10))
values.
Returns a table
showing all the
unique values as
well as a count of
each occurrence.
table(c("a", "a",
table(x, exclude) To include a 2-"a", 1-"b", 1-"c"
"b", "c"))
count of NA
values, include
the
argument exclude
= NULL
Missing (NA) values
• In R, missing data are coded as NA. Most
descriptive statistics functions will freak out if
there is a missing (NA) value in the data. For
example, the following code will return NA as a
result because there is an NA value in the data
vector:
a <- c(1, 5, NA, 2, 10)
mean(a)
## [1] NA
• Function to remove NA values ( na.rm=TRUE)
mean(a, na.rm = TRUE)
## [1] 4.5
Practice Problems
1. Create a vector that shows the square root of the integers from 1 to 10.
2. Renata thinks that she finds more treasure when she’s had a mug of
grogg than when she doesn’t. To test this, she recorded how much
treasure she found over 7 days without drinking any grogg (ie., sober),
and then did the same over 7 days while drinking grogg (ie., drunk). Here
are her results:
day sober drunk
Monday 2 0
Tuesday 0 0
Wednesday 3 1
Thursday 1 0
Friday 0 1
Saturday 3 2
Sunday 5 2
How much treasure did Renata find on average when she was sober? What about
when she was drunk?
3. Using Renata’s data again, create a new vector called difference that shows how
much more treasure Renata found when she was drunk and when she was
not. What was the mean, median, and standard deviation of the difference?
4. There’s an old parable that goes something like this. A man does some work for a
king and needs to be paid. Because the man loves rice (who doesn’t?!), the
man offers the king two different ways that he can be paid. You can either pay
me 100 kilograms of rice, or, you can pay me as follows: get a chessboard and
put one grain of rice in the top left square. Then put 2 grains of rice on the next
square, followed by 4 grains on the next, 8 grains on the next…and so on,
where the amount of rice doubles on each square, until you get to the last
square. When you are finished, give me all the grains of rice that would (in
theory), fit on the chessboard. The king, sensing that the man was an idiot for
making such a stupid offer, immediately accepts the second option. He
summons a chessboard, and begins counting out grains of rice one by one…
Assuming that there are 64 squares on a chessboard, calculate how many
grains of rice the main will receive. If one grain of rice weights 1/6400
kilograms, how many kilograms of rice did he get? Hint: If you have trouble
coming up with the answer, imagine how many grains are on the first, second,
third and fourth squares, then try to create the vector that shows the number
of grains on each square. Once you come up with that vector, you can easily
calculate the final answer with the sum() function.