print(0.1)[1] 0.1
print(1)[1] 1
Welcome to the Basic R Tutorial webpage! This tutorial will give you an easy to follow and example-led approach to understand the foundation of R. You are welcome and encouraged to modify and test with the code here.
Numeric data contain numbers, this also includes decimals.
Integer data are values that do not have decimals.
An integer is just a subset of numeric data. The integer data type can only be whole numbers, while numeric data can be all kinds of numbers.
A string is a sequence of characters that represents text in programming.
Boolean represents a logical data type that can have one of two possible values: TRUE or FALSE. In R, logical values are used to represent the result of comparisons and conditions.
Use the <- operator to assign a value to a variable. You can assign any data type to a variable: numeric, integer, character/string, and logical/boolean.
You can assign variables using the following operators: <-, =, or ->. THe most common (and in our opinion, easiest) way to assign variables uses the <- operator.
Usually we can use these built-in functions: typeof() and class() to determine type of data.
Examples of valid variable names:
Examples of invalid variable names:
Vectors can only contain variables that have the same data type. We call this homogenous data.
A list is a collection of ordered data. It can contain different data types inside it (heterogenous data). Lists can be used for vectors, matrices, list of characters, and list of functions.
[[1]]
[1] 1 2 3 4
[[2]]
[1] "Constantine" "Laura" "Ha" "Snoopy"
[[3]]
[1] 4
[[1]]
[[1]][[1]]
[1] 1 2 3 4
[[1]][[2]]
[1] "Constantine" "Laura" "Ha" "Snoopy"
[[1]][[3]]
[1] 4
[[2]]
[1] 5 6 7 8
[[3]]
[1] "We are all students at GWU"
A matrix is a two-dimensional data set with columns and rows.
An array is a collection of data. The data must all be the same type (homogenous data), have a fixed size, and can be multidimensional.
[1] "Snoopy" "Linus" "Lucy" "Charlie" "Woodstock" "Sally"
, , 1
[,1] [,2] [,3]
[1,] 60 73 90
[2,] 96 86 91
, , 2
[,1] [,2] [,3]
[1,] 89 65 80
[2,] 82 77 50
A data frame is like spreadsheet. It can hold different types of data and have multiple columns and rows.
Names Score Pass_Class
1 Abby 90 TRUE
2 Bob 87 TRUE
3 Chris 67 FALSE
4 Denny 91 TRUE
| Data Structure | Data Types Allowed |
|---|---|
| Vector | Only one data type (homogenous) |
| List | Multiple data types (heterogenous) |
| Matrix | Only one data type (homogenous) |
| Array | Only one data type (homogenous) |
| Data Frame | Multiple data types (heterogenous) |
Simply insert a # sign and then write your comment for your code to enhance its reproducability and clarity. You can also comment out pieces of code to test various methods of completing a task.
On this page, you will find some interview questions to practice your basic knowledge of R programming. All of the content is based on the R Basic tutorial from this page.
Even if you are not preparing for an interview, these questions can help test your knowledge (perhaps for an upcoming exam, certification, or homework assignment!).
Try to actively recall the answers before checking them. This is one of the most effective ways to learn and increase retention!
A. A vector can contain elements of different types, a list cannot.
B. A vector contains elements of the same type, while a list can contain elements of different types.
C. Both vectors and lists can only contain numeric values.
D. There is no difference; they are the same in R.
B. A vector contains elements of the same type, while a list can contain elements of different types.
12months
A. Numeric
B. Boolean
C. Character
D. Integer
C. Character
A. <-
B. //
C. :
D. ( )
A. <-
daysof.week <- c(“Monday1”, “Tuesday”, “Wednesday”, “Thursday”, “Friday” “Saturday”, “Sunday”, 7)
A. The variable name is invalid.
B. There is a missing comma.
C. Arrays need different data types, not the same data types.
D. The array( ) function cannot be used with character data.
B. There is a missing comma.
- Bonus Tip: R doesn’t care that “Monday1” includes a number — it still treats the entire value as a character string. It also “coerces” the 7 to a character string in the vector becaue it must be the
D. Character.
- Bonus Tip: Mixing “Monday1” (a character string) and 7 (a number) in a vector causes R to coerce all elements to character.