100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached
logo-home
Summary FULL GUIDE - Programming with R $8.16   Add to cart

Summary

Summary FULL GUIDE - Programming with R

 244 views  9 purchases
  • Course
  • Institution

All functions required for the workbooks and assignments clearly explained in one document! The order of the document is sorted per week.

Preview 4 out of 39  pages

  • June 3, 2020
  • 39
  • 2019/2020
  • Summary
avatar-seller
Praktische informatie programmeren met R

Import data
Make sure the data is in a .csv file and put it in a dataframe:
df1 <- read.csv("C:/Users/Jeroen/Downloads/Rstudio/Module 3/students1.csv",
stringsAsFactors = FALSE)

NOTE: R uses / in the path, not the Windows OS default \




Useful keyboard shortcuts (first Windows, then Mac):




- Als je iets niet kunt vinden, check Stackoverflow
- Alleen code in de editor, linksboven wordt opgeslagen.
- Namen van Objects/Variables kunnen niet beginnen met een cijfer. 1fruit kan niet.
fruit2 wel.
- Objects kunnen gemaakt worden op twee manieren
- x=12
- x <- 12
- Check what objects are stored in the environment by using
ls()
- Remove your objects by using
rm()
- Short key voor het pijltje is Alt + - (minus teken)
- Data types for objects:

, - Characters/Strings,
- Characters maak je net als in Python met “ “
- Doubles (int/float)
- Boolean (True/Flase)
- Een True statement maak je met X <- 2 == 2
- Een False statement maak je met X <- 2 == 3
- Factor: this data type is for categorial data: Left / Right, Male / Female, VWO,
HAVO, MAVO.
- When you add character vectors to a data frame, they are
automatically converted into factors!. Unless.. you use:
stringAsFactor = FALSE
- The categories in a factor vector are often referred to as levels
- Op deze pagina vind je veel cheat sheets voor programmeren met R
- Als je data wilt importeren, moet je / gebruiken in de path specification ipv de default
\ op Windows OS.
- Je kunt de geïmporteerde df1 en df2 combineren onder de objectnaam ‘students’
dmv het volgende commando:
students <- rbind(df1, df2)
- Als je wilt bekijken welke data types er in de dataset zitten, kun je een kijkje
nemen met het volgende commando: str(students)
- Je kunt kijken naar de dataset structure doormiddel van het volgende commando:
head(students). Hiermee krijg je de eerste 5 regels te zien.
- Vectors: is een set sequence van data elementen met hetzelfde data type
- List/generic vectors: is een set sequence van data elementen met die verschillende
data types kan bevatten.

Functions

- Functions with ‘...’ arguments (= components of a function) in them can take in any
data type and or additional input without claiming an error.
- C-function: combines/concatenates homogeneous data elements into a vector:
friend_names <- c("Bertha", "Herbert", "Alice", "Nathaniel")
- if you combine heterogeneous data types into a vector, it will convert
them to the most reasonable data type, often that is character.
However, if there are no characters in the vector, it will turn to
numeric → booleans become 1 and 0.
- Class-function: To see what data types are in a certain vector you can use
class(friend_names)
- Als je ‘L’ achter de numeric values zet in een vector, dan kun je van float values,
numeric/int values maken
- Je kunt ook vectors maken met een vaste interval doormiddel van de sequence
functie: dec_frac_seq <- seq(from = 10, to = 3, by = -0.2)
- Als de je stappen van 1 wilt, dan kun je ook het volgende commando met de
‘:’ gebruiken: seq <- 28:112
- ‘by’ indiceert de grootte van de stappen in de sequence.
- Met mean(variabel_x) kun je het gemiddelde van een functie zoals een vector of een
sequence berekenen.
- Net als in Excel zijn gebruikt R vergelijkbare termen voor fucnties:

, - sum(ages_numeric)
- min(ages_numeric)
- Smallest value in de vector/sequence
- max(ages_numeric)
- Largest value in de vector/sequence
- prod(ages_numeric)
- Product of the vector/sequence
- median(ages_numeric)
- abs(ages_numeric)
- Absolute values of a vector/sequence
- sqrt(ages_numeric)
- Square root
- round(variable_x, digits = 2)
- round the data elements in the vector to 2 digits
- cos, sin, tan, log, expontential
- cos(ages_numeric)
- sin(ages_numeric)
- tan(ages_numeric)
- log(ages_numeric)
- exp(ages_numeric)
- Indexing can be done in vectors via two ways. Either counting from 0 from
the start [25, 35, 42] OR similarly from the back with a minus sign [-3, -6, -
10]
my_vector[28] → index nr 29 from the e.g. 100 data elements
my_vector[-1] → index nr 99 from the e.g. 100 data elements → 100, 99, 98

- If you want to select certain letters from string u can use subtract
Substr(“stringx”, start = 2, stop = 4)
- If you want to replace certain letters from a string use the chartr() function
chartr("r", "R", bird_name) → “raven” now becomes “Raven”
- You can capitalize the whole string by using the toupper()
toupper(bird_name) → RAVEN


- Combine vectors into a table/matrix
- rbind(friend_names, weight_numeric) → combine rows
- cbind(friend_names, weight_numeric) → combine collumns
- Combine vectors into a data frame by using the Data frame function. It is
important to specify each vector to a column/variable in the data frame.
- friends <- data.frame(names = friend_names, ages = friend_ages,
stringsAsFactors = FALSE)
- When you add character vectors to a data frame, they are
automatically converted into factors!. Unless.. you use:
stringAsFactor = FALSE
- This means you can only add data elements to de factor vector
that are identical to one of the existing levels (= categories).
- If you try to replace/add a non-identical data element it
will result in an N/A

, - Combine vectors into a list
- friends_list <- list(names = friend_names, ages = friend_ages)
- Check number of rows or# columns in a matrix/df
- nrow(matrix)
- ncol(matrix)
- Check the length of a vector: length(friend_names)
- Check for missing values with
is.na(friend_names)
- Omit/remove missing values
na.omit(friend_names)
- You can also perform operations on vectors, for example adding 5 (years)
to each data element in the vectore ages_numeric
- ages_numeric
five_years_older <- ages_numeric + 5L
- Als je een specifiek deel van één kolom/variabele uit je data frame wilt
pakken, gebruik je ‘$’ om de kollom/variabele te selecteren en ‘[10:20]’ om de
gewenste rijen te selecteren
- students$DataYear[10:20]
- If you want to index the whole vector that makes up the column in the
data frame or list/vector in a list, you can also use ‘[[ ]]’ OR type the name
of the column in [“ “]
students[[2]] → show the 2nd column/variable in the data
frame ‘students’
students[“DataYear”] → shows the vector
- Wil je een specifiek deel van meerdere kolommen in combinatie met
meerdere rijen pakken, dan gebruik je de ‘ , ‘ tussen de gewenste rijen en
kolommen. Zie hier wederom het gebruik van c-functie om de kolommen aan
te geven.
- students[21:30, c(2, 4, 5)]
- Selecting a specific part/subset of you vector. kun je de volgende twee
commandos uitvoeren. Zie wederom het gebruik van de c-functie hier. Maar
nu binnen de [ ]
- friend_names[c(1, 3)] → selecteer data element 1 en 3
- friend_names[-c(1, 3)] → selecteer alles, behalve data element 1 en
3 met het ‘-’ teken
- Replace a specific subset in your data frame
- my_df$fruits[3] <- "a new fruit"
- OR adjust the whole vector
my_df$fruits <- c(“apple”, “bannana”, “raspberry”)
- Replacing factor columns needs to be done a little different. First you
convert the column/variable into characters, then you adjust them. If you want
them to change back to levels, you can use the factor() function
- my_df$fruits <- as.character(my_df$fruits)
my_df$fruits[1] <- "raspberry"
factor(my_df$fruits)
- Delete a column from a data frame by selecting the column and setting the

The benefits of buying summaries with Stuvia:

Guaranteed quality through customer reviews

Guaranteed quality through customer reviews

Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.

Quick and easy check-out

Quick and easy check-out

You can quickly pay through credit card or Stuvia-credit for the summaries. There is no membership needed.

Focus on what matters

Focus on what matters

Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!

Frequently asked questions

What do I get when I buy this document?

You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.

Satisfaction guarantee: how does it work?

Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.

Who am I buying these notes from?

Stuvia is a marketplace, so you are not buying this document from us, but from seller jeroenverboom. Stuvia facilitates payment to the seller.

Will I be stuck with a subscription?

No, you only buy these notes for $8.16. You're not tied to anything after your purchase.

Can Stuvia be trusted?

4.6 stars on Google & Trustpilot (+1000 reviews)

62491 documents were sold in the last 30 days

Founded in 2010, the go-to place to buy study notes for 14 years now

Start selling
$8.16  9x  sold
  • (0)
  Add to cart