10 More simple data wrangling
10.1 a nice, fun little matrix for you
#> [,1] [,2] [,3] [,4] [,5]
#> [1,] 1 4 7 10 13
#> [2,] 2 5 8 11 14
#> [3,] 3 6 9 12 15
Write the code that creates this matrix:
Write DIFFERENT code that creates this matrix in an alternate way:
In the matrix above, what does
[,4]
mean?What code would return the value in the 3rd column and 3rd row?
What single line of would give you the average of the all the numbers in columns 2, 4, and 5 and in rows 1 and 3?
turn
x
into a data frame.How do you know you have turned
x
into a data frame?
10.2 More fun (this class is really awesome isn’t it?)
df
#> X1 X2 X3 X4 X5 X6 X7 X8 X9 X10
#> 1 1 11 21 31 41 51 61 71 81 91
#> 2 2 12 22 32 42 52 62 72 82 92
#> 3 3 13 23 33 43 53 63 73 83 93
#> 4 4 14 24 34 44 54 64 74 84 94
#> 5 5 15 25 35 45 55 65 75 85 95
#> 6 6 16 26 36 46 56 66 76 86 96
#> 7 7 17 27 37 47 57 67 77 87 97
#> 8 8 18 28 38 48 58 68 78 88 98
#> 9 9 19 29 39 49 59 69 79 89 99
#> 10 10 20 30 40 50 60 70 80 90 100
Consider the dataframe above called
df
. What would running this code returnsum(df[7,7:10])
How can you tell if an object in R is a dataframe?
How could you create the dataframe above called
df
?What code would return the average of row 2 of
df
?Consider
mtcars
dataset that comes preloaded with R that looks like this:
head(mtcars)
#> mpg cyl disp hp drat wt qsec vs am
#> Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1
#> Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1
#> Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1
#> Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0
#> Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0
#> Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0
#> gear carb
#> Mazda RX4 4 4
#> Mazda RX4 Wag 4 4
#> Datsun 710 4 1
#> Hornet 4 Drive 3 1
#> Hornet Sportabout 3 2
#> Valiant 3 1
- Why do I get this error when I run the code below:
Error in plot(hp, mpg) : object 'hp' not found
?
plot(hp,mpg)
Error in plot(hp, mpg) : object 'hp' not found
Bonus: What is a topic that you find confusing at this point in class?