0% found this document useful (0 votes)
4 views5 pages

Expt1.ipynb - JupyterLab

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

Expt1.ipynb - JupyterLab

Python book
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

10/20/24, 12:13 PM expt1

In [16]: #Exercise 1: Introduction to R,Basic Commands, Graphics, Indexing Data, Loading


#Additional Graphical and Numerical Summaries.

In [17]: # Install a package (e.g., ggplot2 for advanced visualization)


install.packages("ggplot2")

Warning message:
"package 'ggplot2' is in use and will not be installed"

In [18]: # Load ggplot2 library


library(ggplot2)

In [19]: #Basic Commands


# Arithmetic Operations
5 + 3 # Addition
5 - 3 # Subtraction
5 * 3 # Multiplication
5 / 3 # Division
5^2 # Exponentiation

# Creating Variables
x <- 10 # Assign value 10 to x
y <- 5
z <- x + y # Add two variables

# Data Types
a <- 10L # Integer
b <- 3.14 # Numeric (Double)
c <- TRUE # Logical (Boolean)
d <- "R Programming" # Character string

# Check the type of variable


class(a)
class(d)

# Built-in Help
?mean # View documentation for 'mean' function

8
2
15
1.66666666666667
25
'integer'
'character'

localhost:8888/lab/tree/expt1.ipynb 1/5
10/20/24, 12:13 PM expt1

mean {base} R Documentation

Arithmetic Mean
Description
Generic function for the (trimmed) arithmetic mean.

Usage
mean(x, ...)

## Default S3 method:
mean(x, trim = 0, na.rm = FALSE, ...)

Arguments
an R object. Currently there are methods for numeric/logical vectors and date, date-
x time and time interval objects. Complex vectors are allowed for trim = 0 , only.

the fraction (0 to 0.5) of observations to be trimmed from each end of x before the
trim mean is computed. Values of trim outside that range are taken as the nearest
endpoint.

a logical evaluating to TRUE or FALSE indicating whether NA values should be


na.rm stripped before the computation proceeds.

... further arguments passed to or from other methods.

Value
If trim is zero (the default), the arithmetic mean of the values in x is computed, as a
numeric or complex vector of length one. If x is not logical (coerced to numeric), numeric
(including integer) or complex, NA_real_ is returned, with a warning.

If trim is non-zero, a symmetrically trimmed mean is computed with a fraction of trim


observations deleted from each end before the mean is computed.

References
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth &
Brooks/Cole.

See Also
weighted.mean , mean.POSIXct , colMeans for row and column means.

localhost:8888/lab/tree/expt1.ipynb 2/5
10/20/24, 12:13 PM expt1

Examples
x <- c(0:10, 50)
xm <- mean(x)
c(xm, mean(x, trim = 0.10))

[Package base version 4.4.0 ]

In [20]: #Graphics in R
# Scatter plot using the built-in 'mtcars' dataset
plot(mtcars$wt, mtcars$mpg,
main = "Scatter Plot of Weight vs MPG",
xlab = "Weight", ylab = "Miles Per Gallon",
col = "blue", pch = 19) # pch: point shape

In [21]: #Indexing Data


# Create a vector
v <- c(10, 20, 30, 40, 50)

# Access elements
v[1] # First element
v[2:4] # Elements from 2 to 4
v[c(1, 5)] # Elements at positions 1 and 5

10
20 · 30 · 40

10 · 50

In [22]: #Data Frame:


# Create a sample data frame

localhost:8888/lab/tree/expt1.ipynb 3/5
10/20/24, 12:13 PM expt1

df <- data.frame(Name = c("Alice", "Bob", "Charlie"),


Age = c(25, 30, 35),
Score = c(85, 90, 88))

# Access columns
df$Name # Get the 'Name' column

# Access a specific value (row 2, column 3)


df[2, 3]

# Subset rows based on a condition


df[df$Age > 25, ]

'Alice' · 'Bob' · 'Charlie'

90
A data.frame: 2 × 3

Name Age Score

<chr> <dbl> <dbl>

2 Bob 30 90

3 Charlie 35 88

In [23]: # Loading Data


#Load built-in datasets:
data(mtcars) # Load the 'mtcars' dataset
head(mtcars) # Display the first 6 rows
#Load data from a CSV file:
# Assuming 'data.csv' is in the working directory
#my_data <- read.csv("data.csv")
#head(my_data)

A data.frame: 6 × 11

mpg cyl disp hp drat wt qsec vs am gear

<dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>

Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4

Mazda RX4
21.0 6 160 110 3.90 2.875 17.02 0 1 4
Wag

Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4

Hornet 4
21.4 6 258 110 3.08 3.215 19.44 1 0 3
Drive

Hornet
18.7 8 360 175 3.15 3.440 17.02 0 0 3
Sportabout

Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3

In [24]: #Additional Graphical and Numerical Summaries


# Boxplot for MPG
boxplot(mtcars$mpg,
main = "Boxplot of Miles Per Gallon",
ylab = "MPG", col = "lightblue")

localhost:8888/lab/tree/expt1.ipynb 4/5
10/20/24, 12:13 PM expt1

# Histogram for MPG


hist(mtcars$mpg,
main = "Histogram of Miles Per Gallon",
xlab = "MPG", col = "lightgreen")

localhost:8888/lab/tree/expt1.ipynb 5/5

You might also like