0% found this document useful (0 votes)
8 views7 pages

Ex 1

The document provides Python and R commands for various data manipulation tasks, including generating random numbers, removing variables, and performing mathematical operations. It also covers handling NA and Inf values in R, constructing lists and matrices, and creating DataFrames. Each command is accompanied by a brief explanation of its functionality.
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)
8 views7 pages

Ex 1

The document provides Python and R commands for various data manipulation tasks, including generating random numbers, removing variables, and performing mathematical operations. It also covers handling NA and Inf values in R, constructing lists and matrices, and creating DataFrames. Each command is accompanied by a brief explanation of its functionality.
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/ 7

Python Commands

(2a) Create an n-item vector of random normal deviates

python

CopyEdit

import numpy as np

n = 10 # Set n to any desired number

y1 = np.random.normal(size=n)

print(y1)

Explanation:

• np.random.normal(size=n) generates n normally distributed random numbers (mean = 0, std =


1).

(2b) Create another n-item vector that has n added to each random uniform distribution

python

CopyEdit

y2 = np.random.uniform(size=n) + n

print(y2)

Explanation:

• np.random.uniform(size=n) generates n random numbers uniformly distributed between 0 and


1.

• Adding n shifts each value by n.

(2c) Remove a variable y1 from the workspace

python

CopyEdit

del y1

Explanation:

• del y1 deletes the variable y1 from memory.

(2d) Remove all variables from the workspace

python

CopyEdit

globals().clear()

Explanation:
• globals().clear() removes all user-defined variables.

(2e) Find the square root of 16

python

CopyEdit

import math

print(math.sqrt(16))

Explanation:

• math.sqrt(16) computes the square root of 16.

R Commands

(3a) Generate 10 uniformly distributed numbers (0,1)

CopyEdit

x <- runif(10)

print(x)

(3b) Display them using plot()

CopyEdit

plot(x)

(3c) Generate 5 random integers from 1 to 10 with and without replacement

CopyEdit

sample(1:10, 5, replace=TRUE) # With replacement

sample(1:10, 5, replace=FALSE) # Without replacement

(3d) Select 2 random months from a list of months

CopyEdit

months <- month.name

sample(months, 2)
(3e) Combine two strings with comma separation

CopyEdit

paste("Hello", "World", sep=", ")

(3f) Prefix and suffix a word with "A"

CopyEdit

paste("A", "word", "A", sep="")

(3g) Find the number of characters in a string

CopyEdit

nchar("HelloWorld")

(3h) Construct a vector of odd numbers from 1 to 10 using seq()

CopyEdit

odd_numbers <- seq(1, 10, by=2)

print(odd_numbers)

(3i) Find its log (base 10) and sine values

CopyEdit

log10(odd_numbers)

sin(odd_numbers)

(3j) Compute sum, mean, median, length, variance, standard deviation, and quantiles

CopyEdit

sum(odd_numbers)

mean(odd_numbers)

median(odd_numbers)
length(odd_numbers)

var(odd_numbers)

sd(odd_numbers)

quantile(odd_numbers)

(3k) Get summary of the same

CopyEdit

summary(odd_numbers)

(3l) Get today’s date and time

CopyEdit

Sys.Date()

Sys.time()

(3m) Find your age in days

CopyEdit

as.numeric(Sys.Date() - as.Date("YYYY-MM-DD"))

Replace YYYY-MM-DD with your birth date.

(3n) Convert to lower and upper case

CopyEdit

tolower("Hello World")

toupper("Hello World")

(3o) Extract the last three characters of a string

CopyEdit

substr("HelloWorld", nchar("HelloWorld")-2, nchar("HelloWorld"))

(3p) Sort the string in lexicographic order


r

CopyEdit

sort(c("banana", "apple", "cherry"))

(3q) Display strings that start with "A"

CopyEdit

words <- c("Apple", "Banana", "Apricot", "Avocado")

grep("^A", words, value=TRUE)

(3r) Count how many common words start with 't'

CopyEdit

sum(grepl("^t", tolower(words)))

(3s) Count how many common words end with a vowel

CopyEdit

sum(grepl("[aeiou]$", tolower(words)))

(3t) Find all words containing at least one vowel and negate

CopyEdit

words[!grepl("[aeiou]", words)]

(3u) Find all words consisting only of consonants

CopyEdit

words[grepl("^[^aeiou]+$", words)]

(3v) Sort the string in lexicographic order

CopyEdit

sort(words)
Handling NA and Inf Values in R

(4a) Compute mean and median while ignoring NA/Inf values

CopyEdit

x <- c(1:5, NA, 7:10)

y <- c(1:5, Inf, 7:10)

mean(x, na.rm=TRUE)

median(x, na.rm=TRUE)

mean(y, na.rm=TRUE)

median(y, na.rm=TRUE)

(4b) Ignore NA values

CopyEdit

mean(x, na.rm=TRUE)

median(x, na.rm=TRUE)

(4c) Find the length of a NULL object

CopyEdit

length(NULL) # Output: 0

(4d) Construct a list of a numeric vector, logical, string, and another list

CopyEdit

my_list <- list(numbers=c(1,2,3), flag=TRUE, text="Hello", sublist=list(a=1, b=2))

print(my_list)

(4e) Name some/all entries in a list and get object names

CopyEdit
names(my_list) <- c("num_vector", "logic", "str", "nested_list")

names(my_list)

(4f) Create a matrix using matrix() and diag() and perform operations

CopyEdit

A <- matrix(1:9, nrow=3, byrow=TRUE)

B <- diag(3) # Identity matrix

# Matrix operations

t(A) # Transpose

A %*% B # Matrix multiplication

solve(A) # Inverse of A (if it's invertible)

det(A) # Determinant

eigen(A) # Eigenvalues and eigenvectors

(4g) Construct a DataFrame and expand it

CopyEdit

df <- data.frame(Name=c("Alice", "Bob"), Age=c(25,30))

df$Gender <- c("Female", "Male") # Add a new column

df <- rbind(df, data.frame(Name="Charlie", Age=35, Gender="Male")) # Add a new row

print(df)

You might also like