# some interesting data objects
<- c(3,6,9,5,10)
x <- cbind(x, 2*x)
x.mat <- data.frame(x=x,double.x=x*2)
x.df <- list(myVec=x, myDf=x.df, myString=c("hi","bye")) my.list
Class Activity 3
Data objects can range from simple vectors, which are one-dimensional arrays that hold elements of the same type, to more complex structures like lists and data frames that can store heterogeneous data types. R treats everything as an object, from simple numbers and strings to more complex data structures. This concept allows for a high degree of flexibility in data manipulation and analysis. Here are some of the fundamental data objects you’ll encounter in R:
- Vectors: The simplest and most common type of data object in R. They hold elements of the same data type.
- Matrices: Two-dimensional, rectangular data structures that can store data of a single type.
- Data frames: Similar to matrices but can contain different types of data in each column, much like a spreadsheet.
- Lists: Complex data structures that can hold elements of any type, including other lists.
Understanding these data objects and their characteristics is the first step towards mastering data analysis in R. Now, let’s explore some examples of these data objects in action.
Question 1: data types
- What data type is
x
?
Click for answer
Answer:
# code
typeof(x)
[1] "double"
- What data type is
c(x, x/2)
?
Click for answer
Answer:
# code
typeof(c(x, x/2))
[1] "double"
- What data type is
c(x,NA)
? What data type isc(x,"NA")
?
Click for answer
Answer:
# code
typeof(c(x, NA))
[1] "double"
typeof(c(x, "NA"))
[1] "character"
Question 2: Subsetting and coercion
- How can we reverse the order of entries in
x
?
Click for answer
Answer:
# code
rev(x)
[1] 10 5 9 6 3
length(x):1] x[
[1] 10 5 9 6 3
- What does
which(x < 5)
equal?
Click for answer
Answer:
# code
which(x<5)
[1] 1
- Extract the element of x that corresponds to the location in the preceding question.
Click for answer
Answer:
# code
which(x<5)] x[
[1] 3
- What does
sum(c(TRUE,FALSE,TRUE,FALSE))
equal?
Click for answer
Answer:
# code
sum(c(TRUE,FALSE,TRUE,FALSE))
[1] 2
- What does
sum(x[c(TRUE,FALSE,TRUE,FALSE)])
equal?
Click for answer
Answer:
# code
sum(x[c(TRUE,FALSE,TRUE,FALSE, TRUE)])
[1] 22
- What does
sum(x < 5)
equal?
Click for answer
Answer:
# code
sum(x < 5)
[1] 1
- What does
sum(x[x < 5])
equal?
Click for answer
Answer:
# code
sum(x[x < 5])
[1] 3
- Why
dim(x.mat[1:2,1])
returnNULL
whiledim(x.mat[1:2,1:2])
returns a dimension?
Click for answer
Answer:
# code
dim(x.mat[1:2,1])
NULL
dim(x.mat[1:2,1:2])
[1] 2 2
Question 3: Lists
- Using
my.list
, show six ways to write one command that gives the 3rd entry of variablex
in data framemyDf
Click for answer
Answer:
# code
$myDf$x[3] my.list
[1] 9
$myDf[3,1] my.list
[1] 9
"myDf"]$myDf[3,1] my.list[
[1] 9
"myDf"]$myDf$x[3] my.list[
[1] 9
2]]$x[3] my.list[[
[1] 9
2]$myDf[3,1] my.list[
[1] 9
- What class of object does the command
my.list[3]
return?
Click for answer
Answer:
# code
class(my.list[3])
[1] "list"
- What class of object does the command
my.list[[3]]
return?
Click for answer
Answer:
# code
class(my.list[[3]])
[1] "character"
- What class of object does the command
unlist(my.list)
return? Why are all the entriescharacters
?
Click for answer
Answer:
# code
class(unlist(my.list))
[1] "character"