Story Transcript
FUNDAMENTALS OF R PROGRAMMING
1
2
FUNDAMENTALS OF R PROGRAMMING
Balwinder Kaur Dr. D. Sathiya Mrs. V Sreeprada Dr. R. N Satpathy
3
Editor: Ms. M. Leelavathi Book Pen Publishing, Tamil Nadu, India. NOTION PRESS India. Singapore. Malaysia.
This book has been published with all reasonable efforts taken to make the material error-free after the consent of the author. No part of this book shall be used, reproduced in any manner whatsoever without written permission from the author, except in the case of brief quotations embodied in critical articles and reviews. The Author of this book is solely responsible and liable for its content including but not limited to the views, representations, descriptions, statements, information, opinions and references [“Content”]. The Content of this book shall not constitute or be construed or deemed to reflect the opinion or expression of the Publisher or Editor. Neither the Publisher nor Editor endorse or approve the Content of this book or guarantee the reliability, accuracy or completeness of the Content published herein and do not make any representations or warranties of any kind, express or implied, including but not limited to the implied warranties of merchantability, fitness for a particular purpose. The Publisher and Editor shall not be liable whatsoever for any errors, omissions, whether such errors or omissions result from negligence, accident, or any other cause or claims for loss or damages of any kind, including without limitation, indirect or consequential loss or damage arising out of use, inability to use, or about the reliability, accuracy or sufficiency of the information contained in this book. © Authors No part of this shall be reproduced, reprinted or translated for any purpose whatsoever without prior permission of the authors in writing. First Edition: 2022
4
ABOUT AUTHOR First Author Balwinder Kaur is currently an Assistant Professor in Computer Science at Punjabi University Regional Centre for Information Technology and Management, Mohali (Punjab). Balwinder Kaur did her Master of Computer Applications (MCA), in 2009 at Punjabi University Patiala (Punjab) and B.Sc. (Non-Medical), in 2006 from Himachal Pradesh University, Shimla (HP). She has teaching and research experience of more than 12 years in Computer Science. She has presented various papers in national and international conferences. In 2019, she received Best Educator Award. She has many publications in reputed journals. Her areas of interests include Cloud computing, Programming Languages and Artificial Intelligence.
5
Second Author Dr. D. Sathiya, M.S(IT)., M.Phil., Ph.D., working as an Assistant Professor in the Department of Computer Science at Bharathiyar Arts & Science College for Women, Attur(tk), Salem(dt), Tamilnadu, India, Since 2009. She has secured PhD in the Department of Computer Science (2017-2021) at Karpagam Academy of Higher Education, Coimbatore. She did M.Phil. in the School of Distance Education in Computer Science (2004) at Periyar University, Salem. She has published Research articles in Scopus & Web of Science and Conference Proceedings at National and International level. Research interest includes Networking, Cloud Computing and Machine Learning.
Third Author Mrs. V Sreeprada is working as a faculty in computer science in Vasundra Degree and P.G College for women. She has 12 years Of teaching experience. She is presently pursuing her PhD in Data mining From GITAM University. She has published 7 research articles in highly reputed journals. She attended various national and international Conferences. She taught various subjects such as Programming languages, Computer Networks, DBMS, E Commerce, Web programming, and Data mining. Her research interests include Data Mining and Computer networks. She is an author for several books such as Ecommerce, C and C++ and Data Structures with Boolean algebra.
6
Fourth Author Dr. R. N Satpathy (Professor of Computer Science & Engineering.) is presently working as Director of the office of the Vice Chancellor and also Dean of Faculty of Emerging Technologies and Science at Sri University, Odisha. Dr. Satpathy post-graduated in Computer Science & Engineering Applied Mathematics, MBA (HRD) and Industrial Mathematics from National Institute of Technology – Rourkela and Symbiosys and other leading institutes. He has received 2 Ph.Ds. - one in Computational Mathematics from Utkal University and other in Computer Science & Engineering from Fakir Mohan University. He has also received Post-doctoral from National Institute of Technology – Rourkela and D. Sc in Computational Fluid Dynamics from FM University. He is a Senior Fellow member of DRaS, Fellow member of computer Society of India, FIE(I), Fellow of International Council of
Consultants.
Fellow
Charted
Educator
of
CCLP
Worldwide
(An
International Organization having special consultative status with UN ECOSOC, Institutional Membership of UNESCO NEQMAP and UN Academic Impact) and Fellow Member ISRD (International Society of Research & Development), IETE (Institution of Electronics & Telecommunication Engineers, ISLE, (Indian Society of Lighting Engineers) and SESI(Solar Energy Society of India).He is charted engineer of Institution of Engineers and IETE . He is a Professional Engineer of Indian Engineering Council. He has received 9 international awards and 35 National/Regional/State awards. Dr. Satpathy published 55 patents (out of which 10 are granted),175 technical papers in different National and International Journals, 74 papers in proceedings and published 4 books/edited books and completed 11 projects. He has 27 years of teaching/research/ academic administration experience, out of which 14 years as Director / Principal in different Engineering college and Educational Group.
7
ACKNOWLEDGEMENT First and foremost, praises and thanks to the God, the Almighty, for His showers of blessings throughout my work to complete successfully. I am also deeply thankful to my informants. Their names cannot be disclosed, but I want to acknowledge and appreciate their help and transparency during my work. Their information has helped me to complete this work. I also thank my family who encouraged me and prayed for me throughout the time of my writing. Finally, to my loving, caring and supportive husband, Rachhpal Singh: my deepest gratitude. Your encouragement when the times got rough are much appreciated and duly noted. My heartfelt thanks. May the Almighty God richly bless all of you. Balwinder Kaur I would like to thank my Almighty God. I would like to thank my family members for the vibrant support and encouragement. I particularly thank my friend and my colleague Dr Arumugam for providing valuable advise and inspiration. Dr. D. Sathiya
8
There has been a continuous change in the technical field and applications are developed using it. We need to upskills with various technologies. Hope this book gives you the knowledge and have a happy learning. I would like to thank my family members for the constant support and encouragement. I would like to thank the Almighty God. Mrs. V. Sreeprada
I would like to acknowledge the encouragement of the President of Sri Sri University Prof.(Mrs.) Rajita Kulkarniji and the Vice-Chancellor of Sri Sri University Prof. Ajay Kumar Singhji . I would not be able to get my work done without continuous support of my Wife Rakhi and son Darshan & Chetan ...
-Dr. R. N. Satpathy
9
CONTENT C.NO
TOPIC
PAGE
1.FUNDAMENTALS OF R 1.1
INSTALLATION OF R&R STUDIO
1
1.2
FEATURES OF R
1
1.3
VARIABLES IN R
2
1.4
CONSTANT IN R
5
1.5
OPERATORS IN R
6
1.6
R OBJECTS
14 2.VECTORS
2.1
CREATING VECTORS
19
2.2
ACCESSING ELEMENT OF VECTOR
22
2.3
OPERATORS OF VECTOR
23
2.4
VECTOR ARITHMETIC
28
3.CONTROL STATEMENTS 3.1
IF STATEMENT
30
3.2
IF. ELSE STATEMENT
31
3.3
FOR LOOP STATEMENT
32
3.4
NESTED LOOP STATEMENT
33
3.5
WHILE LOOP STATEMENT
34
10
3.6
REPEAT LOOP AND BREAK STATEMENT
34
3.7
RETURN STATEMENT
35
3.8
NEXT STATEMENT
34
4.FUNCTIONS IN R 4.1
ACTUCAL ARGUMENTS
38
4.2
NAMED ARGUMENTS
39
4.3
GLOBAL & LOCAL VARIABLES
40
4.4
ARGUMENT & LAZY EVALUATION
41
4.5
RECURSIVE FUNCTION
44
5.MATRICES 5.1
CREATING MATRICES
47
5.2
ACCESSING ELEMENTS OF MATRIX
48
5.3
OPERATIONS ON MATRICES
51
5.4
MATRIX TRANSPOSE
59
6.STRINGS 6.1
CREATING STRING
62
6.2
PASTE () & PASTE0()
63
6.3
FORMATTING NUMBERS & STRING USING FORMAT
66
6.4
CONCATENATE & PRINT WITH CAT ()
67
6.5
ENCODING STRINGS WITH FORMAT ()
69
11
6.6
STRING MANUPULATION
71
7.LISTS 7.1
LIST IN R
77
7.2
CREATING LISTS
78
7.3
MANIPULATING LIST ELEMENTS
81
7.4
MERGING LISTS
84
7.5
CONVERTING LISTS TO VECTORS
85
8.ARRAYS 8.1
CREATING ARRAYS
86
8.2
ACCESSING ARRAY ELEMENTS
89
8.3
CALCULATIONS ACROSS ARRAY ELEMENTS
92
9.R FACTORS 9.1
UNDERSTANDING FACTORS
94
9.2
CREATING A FACTOR
95
9.3
GENERATE FACTOR LEVEL
96
9.4
MODIFYING FACTORS
97
9.5
FACTORS IN DATA FRAMES
98
10.DATA FRAMES IN R 10.1
DATA FRAMES
100
10.2
CREATING DATA FRAMES
101
12
10.3
ACCESS ROW&COLOUNM
102
10.4
SELECTING THE SUBSET OF DATA FRAMES
103
10.5
EDITING DATA FRAMES
104
10.6
ADDING NEW VARIABLE
105
10.7
DELETING ROW & COLOUNM
107
11.DATA VISUALIZATION 11.1
INTRODUCTION
109
11.2
TYPES OF DATA VISUALIZATION
110
11.3
BARPLOT
110
11.4
HISTOGRAM
113
11.5
BOXPLOT
116
11.6
SCATTERPLOT
119
11.7
HEAT MAP
122
11.8
MAP VISUALIZATION
124
11.9
3D GRAPHS
127
11.10 ADVANTAGES AND DISADVANTAGES OF DATA VISUALIZATION IN R
129
12.STRING PACKAGE 12.1
CREATING STRING
131
12.2
LENGTH OF STRING
131
12.3
ACCESS PORTION
132
13
12.4
CASE CONVERSION
133
12.5
UPDATING STRING
133
12.6
FUNCTIONS IN STRING
134
12.7
REGULAR EXPRESSION
137
13.DPLYR PACKAGE 13.1
OVERVIEW
142
13.2
LOAD DATA INTO DATAFRAMES
143
13.3
VIEWING THE DATA
146
13.4
SUBSET COLOUNMS USING THEIR PORTION
149
13.5
SUBSET ROWS USING THEIR PORTION
151
13.6
REORDERING THE ROWS
154
13.7
PIPE OPERATOR
155
13.8
GROUP OPERATIONS
156
14
PREFACE R is the world's most popular language for developing statistical software: Archaeologists use it to track the spread of ancient civilizations, drug companies use it to discover which medications are safe and effective, and actuaries use it to assess financial risks and keep economies running smoothly. The fundamentals R Programming takes you on a guided tour of software development with R, from basic types and data structures to advanced topics like closures, recursion, and anonymous functions. No statistical knowledge is required, and your programming skills can range from hobbyist to pro. Along the way, you'll learn about functional and object-oriented programming, running mathematical simulations, and rearranging complex data into simpler, more useful formats. You'll also learn to:–Create artful graphs to visualize complex data sets and functions–Write more efficient code using parallel R and vectorization–Interface R with C/C++ and Python for increased speed or functionality–Find new R packages for text analysis, image manipulation, and more–Squash annoying bugs with advanced debugging techniques Whether you're designing aircraft, forecasting the weather, or you just need to tame your data, This book is your guide to harnessing the power of statistical computing.
15