Practical of R
Practical of R
Session :- 2021-2023
Submitted By:- Anurag Sharma
Submitted To:- Dr. Sahil Saharan Mam
Class :- MCA (2nd Sem.)
2
output:-
output:-
len(word)==max_len: longest_word=word
print(longest_word)
output:-
output:-
s=pd.Series([1,2,3])
s.values
output:-
• length of Series
s=pd.Series([0,1,2,3])
len(s)
output:-
4
output:-
• explicity by position
s2.iloc[[3,2]]
output:-
output:-
s[1:6]
output:-
output:-
output:-
output:-
output:-
9
output:-
10
output:-
output:-
output:-
(iii)Remove columns:
• Use del to delete a book column
#deleting column
copy=sp500.copy()
del copy['Bookvalue']
copy[:3]
output:-
output:-
12
output:
output:-
13
• Float64Index
# indexes using float64index df_f64
=pd.DataFrame(np.arange(0,1000,5),
np.arange(0.0,100.0,0.5))
df_f64.iloc[:5]
output:-
• DateTimeIndex
• create a dataframe using index df_date_times
=pd.DataFrame(np.arange(0,len(date_times)),
index=date_times)
df_date_times
output:-
14
output:-
output:-
output:-
output:-
output:-
16
output:-
output:-
17
(i)Mean
import pandas as pd
# Creating the dataframe of student's marks df =
pd.DataFrame({"John - Marks ":[98,87,76,88,96],
"Adam - Marks":[88,52,69,79,80],
"David - Marks":[90,92,71,60,64],
"Rahul - Marks":[88,85,79,81,91]}) df.mean(axis = 0)
output:-
(ii) Median
# Creating the dataframe of student's marks
df = pd.DataFrame({"John - Marks ":[98,87,76,88,96],
"Adam - Marks":[88,52,69,79,80],
"David - Marks":[90,92,71,60,64],
"Rahul - Marks":[88,85,79,81,91]})
df.median(axis = 0)
#calc. the median of the values in each column
omh.median() output:-
(iii) Mode
# Creating the dataframe of student's marks
df1 = pd.DataFrame({"John - Marks ":[98,87,87,76,88],"Adam -
Marks":[88,52,69,79,79],"David - Marks":[90,92,71,71,64],"Rahul -
Marks":[88,85,85,81,91]}) df1.mode() output:-
18
(i)variance
#calc. the variance of the values in each column
df.var()
output:-
output:-
output:-
(Iv) correlation:-
# create dataframe with 3 columns data
= pd.DataFrame({
"column1": [12, 23, 45, 67],
"column2": [67, 54, 32, 1],
"column3": [34, 23, 56, 23]})
# correlation between column 1 and column2
print(data['column1'].corr(data['column2']))
output:-
20
9. Create a package with valid name and create function including mean,
mode,median.
Step:-
1. Click on file menu -> New project.
2. Choose new directory as shown below:-
4. Finally, Give name to your package as statistics and click create project:
5. Now you can see the package named as statistics shown in file folder as shown below:
22
6. Now, Open this package and the interface will shown as:
8. Now, Run this function and the output will be shown as:
23
10.Create a package to insert element in list and traverse each element of list
and show metadata about your package.
Steps:-
1. At first create package named as “mypackage” as we discussed earlier and the interface will
be shown as:
2. Now, Create function() named as Traverse in .R file of R/directory for creating list and
traverse the list as shown below:
3. And, Run the function Traverse() and the output will be shown as below:
24
11.Create a package with object documentation and show output of each step.
Steps:-
1. At first, Create package named as “mypackage” as we discussed in program 1.
2. Create .R file in R/directory and save it as Add.R and Add roxygen comments to your .R file as
shown below:
3. Run command ‘devtools::document()’ to convert roxygen comments into .Rdfiles. The output
will be shown as:
26
4. Now, Go to man/directory of your package and you will see that add.Rd file is created
automatically as shown below:
12. Create a Package with valid commands and show the output and
additionally show the created Vignettes of the application given below:
➢ Create a csv file, import data in dataframe, rename the column and use sorting over a
column, Add one new row and one new column, And finally show the output.
Steps:-
1. After creating package as we discussed in program 9.
2. Create function() named as dataframe in .R file of R/directory, The function will be shown as:
28
3. And Run the function dataframe and the output will be shown as:
29
5. Now, You will see that it will create Vignettes/directory in your package, Open this directory
and click on MyVignette.Rmd file and make changes to it as you want and the interface will
be shown as:
6. Now, click on knit option and an html page open as shown below:
31
13. Create a package with the valid test cases of Factorial program.
Steps:-
1. Write a function named as factorial in .R file of R/directory as shown below:
3. To setup your package to use testthat, Run the command “usethis::use_testthat()” as shown
below:
32
6. Finally, Run your tests using the command “devtools::test()” or directly click on Run Tests
option and the output will be shown as:
1. To setup your package with Rcpp, run the following command “usethis::use_rcpp()” and
output will be shown as
3. Add Rcpp to the LinkingTo and imports fields in the DESCRIPTION of your package as shown
below:
34
4. Once you are set up, the basic workflow, create a new C++, as shown below:
5. Now, Create function in C++ named as checkArmstrongNumber to check the given number is
Armstrong or not as shown below:
35
6. Finally, Run the code and the output will be shown as:
15. Create a program with the valid test cases of Given string and show Output
of each command.
Steps:-
1. Write a function named as Len in .R file of R/directory as shown below:
36
3. To setup your package to use testthat, Run the command “usethis::use_testthat()” as shown
below:
6. Finally, Run your tests using the command “devtools::test()” or directly click on Run Tests
option and the output will be shown as:
38