1. intro to R Flashcards
R def
- R is an open source programming language and software environment for statistical analysis, Excellent visualisation of graphics machine learning and deep learning related to numbers texts and etc
- written by Ross Ihanka And Robert Gentleman at the Department of statistics of University of Auckland in Auckland, New Zealand
- It is freely available under GNU - general public licence and precompiled binary versions are provided for various operating systems like Linux Windows and Mac
features
8
- R is interpreted
- r is platform independent
- r is portable
- It allows branching and looping
- It allows integration with other programming languages such as C, C++, net and python
- It is rich in Library of functions and data sets
- It has effective data handling and storage facility
- It provides a collection of operators for calculations
- It provides large and integrated collection of tools for data analysis and statistical functions
- Easy and effective Mechanisms
adv of R
7
- Open source
- Platform independent
- Machine learning operations
(Allows ML operations such as classification regression for this purpose it provides various packages and features for developing artificial neural network) - New technologies and concepts often appear first in R
- in are everyone is welcomed to provide new packages, bug fixes and code enhancements
- Continuously growing.
Applications of R
- Finance
- banking
- healthcare
- social media
- ecommerce
- manufacturing
Why Use R?
7
It is a great resource for data analysis, data visualization, data science and machine learning
It provides many statistical techniques (such as statistical tests, classification, clustering and data reduction)
It is easy to draw graphs in R, like pie charts, histograms, box plot, scatter plot, etc++
It works on different platforms (Windows, Mac, Linux)
It is open-source and free
It has a large community support
It has many packages (libraries of functions) that can be used to solve different problems
R math— R as calculator
- by operators:
+,-… - built-in functions:(6)
- min()
- max()
- sqrt()
- abs()
- ceiling()
- floor()
- log()
- exp()– Exponential
- Trigonometric functions(sin(), cos(), tan())
Reading data from text files
By default all the output files in rr stored in working directory it is necessary for the file to be present in current aware of King Directory so that arcane read it
functions for reading text files:
1. scan()
Through scan function we can Read data directly from files
scan(file=” “)
-
readLines()
By this function files can be read line by line
Dysfunction is useful for reading text files that may be unstructured or contain nonstandard data
readLines(file, n)
n– Number of lines to be read -
Reading from URL connection
Readlines() Function is also useful for reading lines in web page
eg:
a= url(—)
b= readLines(a)
b -
read.table()
Data such as table spreadsheets rectangular grids have a record within each record so read. Table function is very
read.table(file, header= true, sep=’’ ‘’)
– Useful for only small to medium data sets and cannot work with the larger data sets
Writing to test data format
-
write.table(c,f)
c- Content(to be written)
f- File to be written - write.matrix(c,f)
-
cat()
Alternative to print that lets you combine multiple items into single output - print()
-
sink()
It can be used to send objects and text to the file
It is useful when you want to look at the content of the object or function that may be too big to display on the screen
We need to specify the file name in sync function which marks it as the start of the file this file will be automatically created in the working directory and we need to write the content switch to be written in the file
eg:
sink(file)
sample(1:10, 8 , replace=i)
sink()
i/p as .csv file
Import and export of data in .csv
- In our we can read and write data from file stored inside and outside r environment
- Or can re run right into various file formats like .CSV, excel, Xml etc
- .csv File should be present in current working directly so that R can read it.. Or we can set our own directories and read files from there
getwd() To get the directory in which R workspace is wrking
setwd() To set new working directory
setwd(“/web/com”)
Input as. CSV file:
Csp file is a text file in which values in the columns are separated by commerce
Begin create the file using windows n….nd pasting this data save the file aBegin create the file using windows notepad by copying and pasting this data save the file With CSV extension
eg:
id, name, age
1, ‘‘g’’, 12
2, ‘‘d’’, 17
reading a .csv file
read.csv() Is used to read data from a csv file in the current working directory
- Once the data is read in the data frame we can apply all the functions applicable on data frames
eg:
q<- read.scv(file)
print(q)
writing on csv file
write.csv
d<- read.csv(file)
data<- subset(d, t-> p))
Reading data from disk
- scan()
- readlines()
- read.table()
- read.csv()
- read_file()
Saving data files to disk
- save():