100% de satisfacción garantizada Inmediatamente disponible después del pago Tanto en línea como en PDF No estas atado a nada 4.2 TrustPilot
logo-home
Resumen

Summary FULL GUIDE - Programming with R

Puntuación
-
Vendido
9
Páginas
39
Subido en
03-06-2020
Escrito en
2019/2020

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

Institución
Grado











Ups! No podemos cargar tu documento ahora. Inténtalo de nuevo o contacta con soporte.

Escuela, estudio y materia

Institución
Estudio
Grado

Información del documento

Subido en
3 de junio de 2020
Número de páginas
39
Escrito en
2019/2020
Tipo
Resumen

Temas

Vista previa del contenido

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
$9.06
Accede al documento completo:

100% de satisfacción garantizada
Inmediatamente disponible después del pago
Tanto en línea como en PDF
No estas atado a nada

Conoce al vendedor

Seller avatar
Los indicadores de reputación están sujetos a la cantidad de artículos vendidos por una tarifa y las reseñas que ha recibido por esos documentos. Hay tres niveles: Bronce, Plata y Oro. Cuanto mayor reputación, más podrás confiar en la calidad del trabajo del vendedor.
jeroenverboom Tilburg University
Seguir Necesitas iniciar sesión para seguir a otros usuarios o asignaturas
Vendido
82
Miembro desde
7 año
Número de seguidores
60
Documentos
8
Última venta
2 meses hace

5.0

1 reseñas

5
1
4
0
3
0
2
0
1
0

Recientemente visto por ti

Por qué los estudiantes eligen Stuvia

Creado por compañeros estudiantes, verificado por reseñas

Calidad en la que puedes confiar: escrito por estudiantes que aprobaron y evaluado por otros que han usado estos resúmenes.

¿No estás satisfecho? Elige otro documento

¡No te preocupes! Puedes elegir directamente otro documento que se ajuste mejor a lo que buscas.

Paga como quieras, empieza a estudiar al instante

Sin suscripción, sin compromisos. Paga como estés acostumbrado con tarjeta de crédito y descarga tu documento PDF inmediatamente.

Student with book image

“Comprado, descargado y aprobado. Así de fácil puede ser.”

Alisha Student

Preguntas frecuentes