0% found this document useful (0 votes)
45 views9 pages

Unit 3 Basic Business Analysis Using R

Hk

Uploaded by

Omkar Moze
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
45 views9 pages

Unit 3 Basic Business Analysis Using R

Hk

Uploaded by

Omkar Moze
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 9
sic Business Analysis Using R Fundamentals of R mcr R being object oriented programming language, all variables, data, functions, results etc. shared in the active memory of computer in the form of objects with operators and inctions Which are themselves objects. R is a very flexible simple language. R can be used pteractively. It means we can type any R command at its prompt and that command is cecuted directly. It means it is not necessary to write complete programming codé like in er programming languages such as C, Pascal etc. General rules for issuing command in R. 1. In software every command is to be issued at the R prompt (>). 2. In any R command blank space before or after any operator is not necessary like other programming languages. 3. More than one commands can be issued on the same line by separating them with G). 4, Comment about any command can be included in the command itself by writing the comment after #. Meee : | R provides an excellent in built help facility for getting more information on any specific eature or named function. Following are different ways of getting help. (1) Type appropriate command at R prompt. help (Im) # to get help on command Im OR 2 Im If there is no help available on any function or feature it displays an error message, help (model) No documentation for ‘model’ in specified packages and libraries : you could try help. h (“model”) By default the function help searches in the packages which are loaded in memory. (UI) In most of R versions help is available in HTML (Hyper Text Markup Langrage). This help can be obtained using command help. start () This launches a web browser which allows the browsing of web pages with hyperlinks. (AID help. search command is useful for searching help in various ways. help.search (“correlation”) Itreturns many matches of functions that contains word “correlation”. 35 ‘Business Analysis Using R Fundamentals of R >xexe2 # x is assigned value of expression x+2 >x #xisnow7 i) 7 (Command in R can be separated by either (; ) or new line (enter key) x=2+5iy=2°3:2=3 (enter key) Elementary commands can be grouped together into one command expression by pputting them in braces {_} and then it becomes code in R. fy2x+2:122} Comment can be included anywhere by starting it with #. In such case starting from # till end of that line is treated as a comment. =3 # kis assigned value 3. Ifa command is not complete till the end of line, R gives different prompt. By default + displayed on second and subsequent lines and it continues to read input until the command ntactically complete. R provides a mechanism for recalling and re-executing previous commands. The tical arrow keys on key board | and T can be used to scroll forward and backward tively through command history. Once the command is located in this way the curser be moved within command using horizontal arrow keys on keyboard > < to move at or to left characters of a particular command. Characters can be deleted by [Del] key or ed with other keys as desired. >Is() #lists all objects > Is (pattern = 0 “temp.”) # list all objects with certain words within name (ie. temp.) > lm(x) # removes object x from work space. fo remove all objects from workspace use. > 1m (list = 1s () ) 8 Executing Commands from A File ‘commands are stored in an external file having extension ( .R ) then those may be dat any time in R session by using command source (“commandi.R*) # to execute commands from external file commandL.R. external file specified (command1.R) is assumed to be present in current working. ory, otherwise complete path can also be specified. Source (“path of external file containing commands") source ("D:\windows\mydata\try.R") source’ is also available in R console window in File menu, 37 = During any R session by default the output of R commands is displayege hhowever it can be diverted to a file using command. For example to divert output . % named output . dat use command. 2 sink Coutput.dat") # output is_diverted to file output. dat The above command will divert all subsequent ouiput from console to extem to file, it can delivered b: 4 named “output.dat". Once output is diverted ack 10 cone command Ean exh ot ading and writing in files, R uses working directory. Such working directory ca viewed using, >getwd() Almost all applications used for handling data will export data as delimited file in AS text. This is convenient way to get large data in R. R can read data stored in ASCII text using command read.table. K can also read files in other format. For example, MS-Excel, SAS, SPSS etc. Howe functions required for reading such data are not incfuded in base package. (D A simple way is to create a file in MS-Excel and save it as (tab delimited) text fe can read such file using read table command. > z= read.table("c:\try.data", header = T) This command reads file try.data stored on C : and with headings. If no headings are present in file use command > z= read.table ("c:\try.data") read.table after reading creates data frame named as V1, V2, ... If the Excel files are already created then such files also can be imported in R. Excel are popular and commonly used in data analysis - R supports a library named “readed import Excel data sheets. To install readx] install R software with r-conda-essential, then’ library readxl is automatically installed. If the package does not exists install it wit library, Use the command. > library (readxl) (bere z) and variables in data frame will 38 se Baniness Analysis Using diver ou \ renee aeet put is_diverted to file output. day —~. Fe des tbseaHent Output from console io ego JS diverted o file, it can detivered beak DA simple way is o create a file in MS-Excel and save it ag (Gab delimited) text fig an read such file using read table command, > 2 read.table(‘c:\try.data’, header = 7) This command reads file try.data stored on C : and with headings, Tfno headings are present in file use command > Z= read.table (“c:\try.data") Tead.table after reading creates data frame (here 2) and variables in data frame will, named as V1, V2, ... If the Excel files are already created then such files also can be imported in R. Exealf are popular and commonly used in data analysis - R supports a Whrary named “readet import Excel data sheets. To install readxl install R software with r-conda-essential, then library readxl is automatically installed. If the Package does not exists install it vib! library. Use the command. > library (readxl) 38 ’ Basic Business Analysis Using Fundam, et, read_excel()retums NA when a symbol without numerical value appears heart can count the number of missing values with the combination of two functions Suny | 0, is.na(). arguments which can be seen through help () command {TD To import files from SPSS into R you need to install Package named “foreign” See: the command > read SPSS (Ble name):.T6 display the data an data frame in R argument to data frame = TRUE in read. SPSS () command 7 (V) To read data from SAS file into R first load the kage “sasTbdat” command read.sasTbdat ( ). 2 ee Following example illustrates the use of SQL in R. Steps : (1) Install and load package sqldf > install packages (“sqldf"). > library (sqld®). (2) Create a dataframe, > pcode = ¢ (“a", “b”, “c”, “a”, “e”, “f"), > qty: ¢ (200, 500, 150, 300, 460, 250) > dp = data frame (“product” = pcode, “Qty” = qty) > dp displays data frame dp Name Quantity a 200 b 500 c 150 3.10 {sic Business Analysis Using R Fundamentals of R d 300 e 450 f 250 | To extract first three rows of above data frame use > x = sqldf (“select * from dp limit 3") >x # display first three rows. Name Quantity 200 b 500 c 150 To extract rows with quantity > 300 use > x1 = sqlaf (“select X from dp where quantity > 300”) > xi = all displays rows with quantity > 300 Name Quantity b 500 3 460 To add a column with quantity + 100 in dp use > xo = sqldf ("select x, quantity + 100 as new quantity from dp”) > xq = Now column quantity + 100 is added in dataframe dp. Name Quantity . New quantity a 200 300 b 500 600 c 150 250 a 300 400 e 460 ree ee “The data sets that come with R or one of the packages are known as resident data sets or built data sets. For accessing existing data sets, data command is used. data(data set name) data (Orange) # To access data Orange. Bad

You might also like