Basic R Flashcards

1
Q

Write a function drawing x times with replacement from the standard normal distribution, then taking an average and printing it

A

myfunction

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Load all functions in the R file “functions.R” in the working directory

A

source(“functions.R”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Show working directory

A

getwd()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Show contents of working directory

A

dir()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Choose working directory

A

setwd()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Get help for the source() function

A

?source

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Find help for linear regressions

A

help.search(“linear regression”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are the five atomic classes of objects

A

character, numeric, integer, complex and logical

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the difference between a list and a regular vector

A

The list may contain elements of different classes, the elements of a vector all have to be the same class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Assign the integer 1 (not the number) to the object x

A

x

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Find out what class x is

A

class(x)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Divide 1 by infinity

A

1/Inf

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Give examples of object attributes

A

Names, dimnames, dimensions, class, length, etc.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Create an empty numeric vector x with 100 elements

A

x

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Input the letters a, b and c as elements in a vector x

A

x

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What happens if you concatenate elements of different classes?

A

They are all coerced to one class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Make a vector consisteng of the integers 1 to 10 to a character format

A

x

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What happens if you run

x

A

NAs are introduced by coercion for the first and second elements

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Create a matrix x of zeroes with two rows and two columns

A

x

20
Q

create two vectors containing the integers 1 to 3. Merge them into a matrix as rows and into a matrix as columns

A

x

21
Q

Transform a vector containing the integers 1 to 10 into a matrix with two rows.

A

x

22
Q

What are the two types of factors and what distiguishes them?

A

Unordered (categorical, unranked), ordered (categorical, ranked)

23
Q

Why is it better to use factors than integer categories

A

They are self-describing

24
Q

Make a factor vector containing five yes/no elements. Get a frequency count of each factor level.

A

x

25
Q

What is the difference between NA and NaN?

A

NA missing values

NaN undefined mathematical operations (subset of NA)

26
Q

Test which elements in the vector x are NAs and which are NANs

A

is. na(x)
is. nan(x)

Returns logical vectors with corresponding elements TRUE if the element in the original vector is missing

27
Q

What are data frames used for?

A
  1. Tabular data
  2. The columns do not have to be the same class
  3. The rows have names
28
Q

Create a data frame x with a “letters” column consisting of the first four letters of the alphabet and a “numbers” column consisting of the first four integers. Give the rows the names “first”, “second”, etc…

A

x

29
Q

Create a vector x with the integers from one to three. Give the elements names.

A

x

30
Q

What are the most important arguments in read.table() function?

A

file, header, sep, colClasses, nrows, skip, stringsAsFactors

31
Q

How large a dataset can you load with read.table() and read.csv()?

A

However much RAM you have available

32
Q

What is the main advantages of dump() or dput() relative to write.table() or write.csv()

A

They preserve R metadata (like class) and work better with version control applications like Git

33
Q

Save the dataframe x in a textual format that preserves metadata. Then read it into r again

A

dput(x, file=”x.R”)

dget(“x.R”)

34
Q

Save the dataframes x and y in a textual format that preserves metadata. Removet hem and then read them into r again

A

dump(c(“x”,”y”), file=”data.R”)

source(“data.R”)

35
Q

What is the main difference between dput() and dump()?

A

Dump() works on several objects, dput() only works on one. Source() will pull in the objects as they were, dget() requires you to assign them anew

36
Q

How do you get a website as text loaded into r

A

y

37
Q

What are the three subsetting operators and how are they different?

A
[ object of same type
[[ single element, class may differ, allows using calculated values, makes subsetting of a list by c( , ) sequential
$ extracts by name, does partial matching by default
38
Q

Create a vector x with the letters from a to d. Now create the vector y consisting of letters in x after b

A

x “b”]

39
Q

Create a vector x with the letters from a to d. Now create the vector y of logicals answering “y>a?” Use y to subset x

A

x “a”

x[y]

40
Q

Make a list x consisting of 1) The number from 1 to 3, 2) the letters a and b and 3) TRUE. Name the elements “num”, “let” and “log”.

Extract the first element in the list by its name, then extract as a list, then extract only its contents.

Exctract the third element of the first element

A

x = list(1:3, c(“a”,”b”), TRUE)

names(x)

41
Q

Make a vector with three words as elements. Extract the first and third word with one command.

A

x

42
Q

Make a 2x2 matrix x containing the numbers 1 to 4. Order numbers first by columns, the by rows. Subset the four, but keep it as a matrix.

A

x

43
Q

Create a vector x with numbers and NAs. Subset all non-NAs as a vector y.

A

x

44
Q

Create a data frame with two numeric variables with a few NAs. Subset out the data frame y consisting of the complete cases.

A

x

45
Q

Draw from a standard normal distribution and assign to object x. Make a logic statement that assigns 1 to y if x is above 0 and 0 to y otherwise.

A

x