# Data Camp: Intro to R Flashcards

1
Q

vector is it array?

A

Vectors are one-dimension arrays that can hold numeric data, character data, or logical data

2
Q

A cheat on how to find those element of vector > 0

A
```# Poker and roulette winnings from Monday to Friday:
poker_vector  0```
```# Select from poker_vector these days
poker_winning_days```
3
Q

Why matrix is called 2 dimentional?

A

Since you are only working with rows and columns, a matrix is called two-dimensional.

You can construct a matrix in R with the matrix() function. Consider the following example:

4
Q

how to define matrix and its contents?

A

matrix(1:9, byrow = TRUE, nrow = 3)
In the matrix() function:

The first argument is the collection of elements that R will arrange into the rows and columns of the matrix. Here, we use 1:9 which is a shortcut for c(1, 2, 3, 4, 5, 6, 7, 8, 9).
The argument byrow indicates that the matrix is filled by the rows. If we want the matrix to be filled by the columns, we just place byrow = FALSE.
The third argument nrow indicates that the matrix should have three rows.

5
Q

A

Similar to vectors, you can add names for the rows and the columns of a matrix

rownames(my_matrix)

6
Q

Which function to calculate rowsum?

A

In R, the function rowSums() conveniently calculates the totals for each row of a matrix. This function creates a new vector:

7
Q

How can you combine 2 vectors?

A

rbind(x,y)

8
Q

How to calculate all the colum in matrix?

A

colSums(all_wars_matrix)

9
Q

How to do matrix selection? or in all colum or in all rows?

A

my_matrix[1,2] selects the element at the first row and second column.
my_matrix[1:3,2:4] results in a matrix with the data on the rows 1, 2, 3 and columns 2, 3, 4.
If you want to select all elements of a row or a column, no number is needed before or after the comma, respectively:

my_matrix[,1] selects all elements of the first column.
my_matrix[1,] selects all elements of the first row.

10
Q

Example of how factor() diff nominal or ordinal categorical variable?

A
```# Animals
> animals_vector  factor_animals_vector  factor_animals_vector
[1] Elephant Giraffe  Donkey   Horse
Levels: Donkey Elephant Giraffe Horse
>
> # Temperature
> temperature_vector  factor_temperature_vector  factor_temperature_vector
[1] High   Low    High   Low    Medium
Levels: Low < Medium < High```

If you don’t specify the levels of the factor when creating the vector, R will automatically assign them alphabetically

11
Q

How to see exact content of a vector wiith string even with without escape xters ?

A

use writeLine()

12
Q

how to show first and last part of ibservation in R?

A

So how to do this in R? Well, the function head() enables you to show the first observations of a data frame. Similarly, the function tail() prints out the last observations in your data set.

Both head() and tail() print a top line called the ‘header’, which contains the names of the different variables in your data set.

13
Q

How do u find the structure of ur dataframe? for example number of variable , observations , variable names , data types of each variable?

A

str(). The function str() shows you the structure of your data set. For a data frame it tells you:

The total number of observations (e.g. 32 car types)
The total number of variables (e.g. 11 car features)
A full list of the variables names (e.g. mpg, cyl … )
The data type of each variable (e.g. num)
The first observations

14
Q

Whe you first recieved a data frame , what d u need to do to see the llok of the data and its contents?

A

Applying the str() function will often be the first thing that you do when receiving a new data set or data frame. It is a great way to get more insight in your data set before diving into the real analysis.

15
Q

How to create dataframe?

A

planets_df

16
Q

How do we subset dataframe?

A

same as we do with matrix. [ ]. By using a comma, you can indicate what to select from the rows and the columns respectively. For example:

my_df[1,2] selects the value at the first row and second column in my_df.
my_df[1:3,2:4] selects rows 1, 2, 3 and columns 2, 3, 4 in my_df.

Sometimes you want to select all elements of a row or column. For example, my_df[1, ] selects all elements of the first row. Let us now apply this technique on planets_df!

17
Q

What is advantage of selecting element of datafarme with variable name?

A

Suppose you want to select the first three elements of the type column. One way to do this is

planets_df[1:3,2]

A possible disadvantage of this approach is that you have to know (or look up) the column number of type, which gets hard if you have a lot of variables. It is often easier to just make use of the variable name:

planets_df[1:3,”type”]

18
Q

Printing entire column from dataframe can be done using subsetiing []. What is short way of achieving this?

A

You will often want to select an entire column, namely one specific variable from a data frame. If you want to select all elements of the variable diameter, for example, both of these will do the trick:

planets_df[,3]
planets_df[,”diameter”]
However, there is a short-cut. If your columns have names, you can use the \$ sign:

planets_df\$diameter

19
Q

technique of selecting all colums with TRUE or FALSE (some value)

A

rings_vector in the console, you get:

[1] FALSE FALSE FALSE FALSE TRUE TRUE TRUE

```# Adapt the code to select all columns for planets with rings
planets_df[rings_vector, TRUE].```

You selected a subset from a data frame (planets_df) based on whether or not a certain condition was true (rings or no rings

Best solution

subset(my_df, subset = some_condition)

The first argument of subset() specifies the data set for which you want a subset. By adding the second argument, you give R the necessary information and conditions to select the correct subset.

Not only is the subset() function more concise, it is probably also more understandable for people who read your code. Continue to the next exercise.

20
Q

What is order() ?

A

order() is a function that gives you the ranked position of each element when it is applied on a variable, such as a vector for example:

> a order(a)
[1] 2 1 3

10, which is the second element in a, is the smallest element, so 2 comes first in the output of order(a). 100, which is the first element in a is the second smallest element, so 1 comes second in the output of order(a).

This means we can use the output of order(a) to reshuffle a:

> a[order(a)]
[1] 10 100 1000

21
Q

Comparison of 3 Vectors , Matrix and Dataframe? Why List differ from them?

A
```Vectors (one dimensional array): can hold numeric, character or logical values. The elements in a vector all have the same data type.
Matrices (two dimensional array): can hold numeric, character or logical values. The elements in a matrix all have the same data type.
Data frames (two-dimensional objects): can hold numeric, character or logical values. Within a column all elements have the same data type, but different columns can be of different data type.
Pretty sweet for an R newbie, right? ;-)```

A list in R allows you to gather a variety of objects under one name (that is, the name of the list) in an ordered way. These objects can be matrices, vectors, data frames, even other lists, etc. It is not even required that these objects are related to each other in any way.

22
Q

how to create named list?

A

my_list

23
Q

[[ or [ which one is use for vector and which is used for List?

A

Important to remember: to select elements from vectors, you use single square brackets: [ ]. Don’t mix them up!

You can also refer to the names of the components, with [[ ]] or with the \$ sign. Both will select the data frame representing the reviews:

shining_list[[“reviews”]]
shining_list\$reviews

Besides selecting components, you often need to select specific elements out of these components. For example, with shining_list[[2]][1] you select from the second component, actors (shining_list[[2]]), the first element ([1]). When you type this in the console, you will see the answer is Jack Nicholson

24
Q

how to print out vector in list and print element of that vector?

A
```# Print out the vector representing the actors
shining_list[["actors"]]```
```# Print the second element of the vector representing the
shining_list[["actors"]][2]```
25
Q

How to add new element to a list?

A

To conveniently add elements to lists you can use the c() function, that you also used to build vectors:

ext_list