Skip to content

Final R class Yay #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions vijieaswar/intror/Challenges.R
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,8 @@ lapply(df, range, na.rm=T)
sapply(df, range, na.rm=T)
#the sapply displays the range min and max value in 2 rows

mapply()
?mapply (not in class)

Question8 <- tapply(df$hp, df$gear, max, na.rm=T)
Question8
66 changes: 66 additions & 0 deletions vijieaswar/intror/Practice.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@

cat <- "cat"
alpha <1
int <-3L
#for integer, you have to have the L at the end, else it will be treated as a numeric

#vectors are a sequence of objects that all have the same class. R will convert some of them if you have a word along with other numerics

list is a special type of vector that can have objects of different classes.

matrices are vectors with a deminsion attributes

x = matrix (1:6, nrow=2, ncol=3)

dim(x)
attributes(x)

x <- 1:10
dim(x) <- c(2,5)
# we added a dimension to create a matrix
# the above does the same as the command below
y <- matrix(1:10, nrow=2, ncol=5)
y
#you can also create a matrix with cbind

x <- 1:3
y <- 10:12
#binding by coloumn
x1 <- cbind(x,y)
x1
#binding by row
x2 <- rbind(x,y)
x2

#matrices all have to be the same class- cant mix char, numeric. dataframe acn have mixed classes
#Factors is another class- can label

unclass(x) is more descriptive-

num <- c(1,1,2,2,3)
fact <- factor(num, level=c(1,2,3), labels=c("yes","no","maybe"))
#database has intergers and you are giving it an atrribute by giving it labels and saying what it means


datafram: each column has to be the same type of data

(x <- data.frame(foo=1:4,bar=c(T,T,F,F)))

attributes are like metadata
adding information to your data without adding more values in the ddata.class(

datafames have row.names

you could give specific attributes such as names etc

x <-1:3
names(x)
#there wont be any names

names(x) <- c("alpha", "beta","gamma")
#the above does not give a data fram
names(x)
str(x)
#it is not a factor but it is assigning names- it is making a label
names function unique to the person but factor labels will replace any value = to that by that character

1 change: 0 additions & 1 deletion vijieaswar/plotting/Classwork.R
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ library(reshape2)

#cor returns a matric, not a df
#creating a heatmap

mtcars %>%
select(c(1,3,4,5,6,7)) %>%
cor() %>%
Expand Down
75 changes: 75 additions & 0 deletions vijieaswar/rmarkdown/Challenge.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
---
title: "Challenge"
author: "Viji"
date: "November 23, 2015"
output:
html_document:
fig_caption: true
number_section: true
toc: true
---



```{r, echo = FALSE}
library(dplyr)
library(tidyr)
library(pander)
library(captioner)
library(ggplot2)
library(rmarkdown)

knitr::opts_chunk$set(echo = FALSE, warning = FALSE)
setwd("~/Desktop/practice-2015-10/vijieaswar/rmarkdown")
```

```{r}
ds <- as.data.frame(state.x77) %>%
add_rownames() %>%
tbl_df()

str(state.x77)

names(ds)


```

# Brief Description
The dataset consists of demographic information from `r dim(ds)[1]` states for the following `r dim(ds[-1])[2]` variables: `r names(ds[-1])`. The mean population across all states is `r round( mean(ds$Population),2)` and the standard deviation is `r round(sd(ds$Population),2)`

```{r}

figNums <- captioner(prefix = 'Figure')
state_cap <- figNums('stateLitIncome', 'Income varies across illiteracy')

```

# Plots
```{r, echo=FALSE, fig.cap=state_cap, dpi=150}
ds %>%
ggplot(aes(x=Illiteracy, y=Income))+
geom_point()+
labs(plot.title="Income across illteracy", x="Illiteracy", y="Income")


```

#Tables
```{r}

tabNums <- captioner(prefix = 'Table')

ds %>%
gather(Variable, Value, -rowname) %>%
group_by(Variable) %>%
summarise(Means = mean(Value) %>% round(2),
SD = sd(Value) %>% round(2)) %>%
pander(caption = tabNums('Demodata', 'Demographic data of 50 states'))
```

```{r, eval=FALSE}
render('Challenge.Rmd', c('word_document', 'html_document'))
```


Binary file added vijieaswar/rmarkdown/Challenge.docx
Binary file not shown.
143 changes: 143 additions & 0 deletions vijieaswar/rmarkdown/Challenge.html

Large diffs are not rendered by default.

134 changes: 134 additions & 0 deletions vijieaswar/rmarkdown/Practice.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
---
title: "Practice"
author: "Viji"
date: "November 23, 2015"
output:
html_document:
fig_caption: true
toc: true
---
```{r}
#in the YAML, add the following
#bibliography: give the location of the bib file
#for a particular style
#csl: give the location of the bib file citation style language. can be found in github

```

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.

When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

# Intro

## Citing a study
```{r}



#how to enter here from papers. need to export the bib key
#@some bibkey
#[] will make the round bracketts around the citation.
```

```{r}


#* makes the text in italics
#** on either side will bold words
#``` is a code block

#` having things in line
# > indicates that it is goin to be quote
# @ with bib key will extract refs

#YAML header is for R to know what to do. YAML has o be closed in ---

#pander, kable creates tables
#ggplot qplot creates figure

#conrol+shift+K will knit html
```

```{r, echo=FALSE}
library(pander)
library(captioner)

#install.packages('captioner', type = 'source') if you get an error message while installing

knitr::opts_chunk$set(echo = FALSE, warning = FALSE)
#Can use this above ling to apply to every chunk

#table option 1
knitr::kable(summary(cars))


#table option 2
tabNums <- captioner(prefix = 'Table')
figNums <- captioner(prefix = 'Figure')
cars_cap <- figNums('carsFig', 'This is a caption for the figure')


pander(lm(cars), caption = tabNums('tab1', 'This is the caption for table 1')) #first coloumn as Y, and second column as X. if you have many columns, 2nd 3rd columnds are X2, X3 etc.


```

REference to **`r tabNums('tab1', display = 'cite')`**.

You can also embed plots, for example:

```{r, echo=FALSE, fig.cap=cars_cap}
#have to write fig.caps = cars_cap because knitr is finicky

plot(cars)
```

```{r}
#Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.

#`r will take the following stuff within ` as a command. if you dont write r after ` it will take it as text
#compare `r mean(cars$dist)` with `mean(cars$dist)`
```

#Tables
```{r}
library(dplyr)
library(tidyr)

cars %>%
gather(Measure, Value) %>%
group_by(Measure) %>%
summarise(means =mean(Value) %>% round(2)) %>%
pander()

```

# Figures
## Scatterplot

```{r, message = FALSE, fig.height=10, fig.width=5, dpi=150, dev='png'}
library(ggplot2)
qplot(dist,speed, data = cars, geom= 'point')

```

This is a list

- There is `r length(cars)` rows in `cars`
- There are `r dim(cars)[2]` variables in `cars`

you would add ` ` around the cars to differentiate it from regular text

This is a quote:

> to be or not to be

```{r}
#command+option+i is a new R chunk
#command+alt+c runs the R chunk


```

# References
Binary file added vijieaswar/rmarkdown/Practice.docx
Binary file not shown.
274 changes: 274 additions & 0 deletions vijieaswar/rmarkdown/Practice.html

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions vijieaswar/wrangling/ChallengesVE.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

#Date: 9th Nov 2015

Loading