---
title: "Intro into R"
author: "vitaly.belik@fu-berlin.de"
output:
  html_document:
    depth: 3
    fig_caption: yes
    highlight: tango
    number_sections: yes
    theme: paper
    toc: yes
  pdf_document:
    number_sections: yes
    toc: yes
---


```{r setup, include = FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# Basics

## Show the current directory
```{r}
getwd()
```
## Creating arrays of values
```{r}
y <- c(1,10)
y
u <- c(1:10)
u
v <- u^2
v
```
## Creating dataframe
```{r}
df <- data.frame(u,v)
df
summary(df)
```
## Writing dataframe to file
```{r}
write.csv(df, file = "MyData.csv")
list.files()
```
## Reading data from file
```{r}
ndf <- read.csv("MyData.csv")
colnames(ndf)
summary(ndf)
```
## Deleting a column
```{r}
Data <- subset( ndf, select = -X )
Data
```
## Adding a column
```{r}
Data['w'] <- v-u
Data
```
## For-loop and if-condition
```{r}
for (i in c(1:10)){
  print(c("index :",i))
  if ((i > 3) & (i<10)){
    print("within if-condition")
    }
}
```
## Subsetting / filtering data
```{r}
Data[Data$u == 1,]
Data[Data$u == 2,]
?subset
subset(Data, u > 1)
```
## Creating plots
```{r}
plot(u,v)
```

# Working with real Data and using grammar of graphics (ggplot)
```{r}
library(ggplot2)
```

