<jupyter_start><jupyter_text>R.1 Review questions from Disc1_intro- Quotes: You can use either double ("") or single quotes ('')- If you want to keep track of the original code you can always open up a second tab to write code  R.2 Review of functions used in Disc1_intro- `Function:` read.csv("NAMEOFFILE.csv")    - Reads csv- `Function:` head(DF_NAME,  of lines you want to display)    - Show first lines of data frame- `Function:` names(DF_NAME)    - Shows column names- `Function:` summary(DF_NAME)    - Shows summary stats- `Function:` mean(DF_NAME$\mathit{\$}$Col_Name)    - Shows mean of certain column- `Function:` sd(DF_NAME$\mathit{\$}$Col_name)    - Shows standard deviation of certain column  Problem set 1- Below are the functions necessary to complete problem set 1  A.1 Import data<jupyter_code>#Imports internal data set
vote_df = read.csv('vote.csv')<jupyter_output><empty_output><jupyter_text>A.1.1 Show the first 2 rows of mtcars<jupyter_code>head(vote_df,2)<jupyter_output><empty_output><jupyter_text>A.2 Import dplyr- Note you do not have to use DPLYR to complete the assignment!- I want to introduce you to the idea of libraries!    - Libraries simply allow you to use more functions!- If you want to know more about DPLYR please visit this [tutorial](https://github.com/corybaird/Development_economics/blob/master/TA_Sessions/R/W1_DPLYR/W1_DPLYR_code.ipynb) I made for a development economics course   A.2.1.1 Install library Common mistake!--Confirm you have installed libraries- Usually you only have to do this once!<jupyter_code>install.packages('dplyr')<jupyter_output><empty_output><jupyter_text>A.2.2 Import library<jupyter_code>library(dplyr)<jupyter_output>Attaching package: ‘dplyr’

The following objects are masked from ‘package:stats’:

    filter, lag

The following objects are masked from ‘package:base’:

    intersect, setdiff, setequal, union<jupyter_text>1. Frequency tables`PS1 corresponding problem: 1b`  1.1 Create table  1.1.1 Create table: DPLYR- DPLYR-Count- `Function:` DF_Name %>% count(Column_Name)<jupyter_code>vote_df %>% 
count(education)<jupyter_output><empty_output><jupyter_text>1.1.1.1 Save output to csv<jupyter_code>vote_df %>% 
count(education) %>% 
write.csv('Education.csv', row.names=FALSE)<jupyter_output><empty_output><jupyter_text>1.1.2 Create table: table function`Function:` table(DF_NAME$\mathit{\$}$Col_Name)<jupyter_code># data: DFNAME$COLUMNNAME
# function: table()
table_ans = table(vote_df$education)
table_ans<jupyter_output><empty_output><jupyter_text>1.2 Create relative frequency table- The same data as above but turned into a percentage  1.2.2 Create table: table function<jupyter_code>prop.table(table_ans)<jupyter_output><empty_output><jupyter_text>1.2.1 Create table: DPLYR- DPLYR-Count-mutate<jupyter_code>vote_df %>% 
count(education) %>% 
mutate(
    Percentage = n/sum(n) 
      )<jupyter_output><empty_output><jupyter_text>2. Histogram- `PS1 corresponding problem: 1c` 2.2.1 Create histogram: histogram function- `Function:` hist(Data_frame$\mathit{\$}$Column, xlab="", ylab="", main="")<jupyter_code>hist(vote_df$income, xlab="Name this label", ylab="Name that label", main="Title")<jupyter_output><empty_output><jupyter_text>2.2.2 Create histogram: ggplot2- ggplot2 is another library you must install like DPLYR<jupyter_code>#install.packages('ggplot2') # If you have not installed
library(ggplot2)
vote_df %>% 
ggplot(aes(x=income)) +  #x= Column_name
geom_histogram(color="black", fill="white", bins=10)<jupyter_output><empty_output><jupyter_text>3. Summarise- `PS1 corresponding problem: 1e`  3.1 Summary stats: summary()- `Function:` summary(DF_NAME$\mathit{\$}$Col_Name)    - Reads csv<jupyter_code>summary(vote_df$income)<jupyter_output><empty_output><jupyter_text>3.2 Summary stats: dplyr<jupyter_code>vote_df %>% 
select(income, education) %>% 
summary()<jupyter_output><empty_output><jupyter_text>4. Groupby  4.1 Summary stats: mean, sd function<jupyter_code>#mean
female_mean =  mean(vote_df$income[vote_df$sex==1])

#sd
female_sd = sd(vote_df$income[vote_df$sex==1])

#Put both results in the same table
summary_table = cbind(female_mean, female_sd)
summary_table<jupyter_output><empty_output><jupyter_text>4.2 Summary stats: dplyr--groupby<jupyter_code>vote_df %>% 
group_by(sex) %>% 
summarise(
    avg_wt = mean(income),
    sd_wt = sd(income))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12234649623373658, 'fraction_numerical': 0.016206345583200183, 'mean_word_length': 6.490598290598291, 'pattern_counts': {'":': 0, '<': 60, '<?xml version=': 0, '>': 71, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 1, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '15', 'dir': 'jupyter-structured-clean-dedup', 'id': '593894', 'license': 'MIT', 'n_tokens_mistral': 1593, 'n_tokens_neox': 1516, 'n_words': 367, 'path': 'R_basics/Disc2_PS1.ipynb', 'repo_name': 'corybaird/PLCY_Code_2020_winter', 'size': '4381'}