0% found this document useful (0 votes)
3 views8 pages

r programming built in functions

The document outlines various built-in functions in R categorized into mathematical, statistical, character, data manipulation, logical, input/output, date/time, and utility functions. Each category includes examples demonstrating their usage, such as calculating mean and standard deviation, string manipulation, and data reading/writing. The comprehensive overview highlights R's versatility for data analysis and manipulation tasks.

Uploaded by

reshmasample987
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
3 views8 pages

r programming built in functions

The document outlines various built-in functions in R categorized into mathematical, statistical, character, data manipulation, logical, input/output, date/time, and utility functions. Each category includes examples demonstrating their usage, such as calculating mean and standard deviation, string manipulation, and data reading/writing. The comprehensive overview highlights R's versatility for data analysis and manipulation tasks.

Uploaded by

reshmasample987
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 8

R provides a rich set of built-in functions that can be used for various purposes like mathematical

computations, data manipulation, statistical analysis, and more. Here are some commonly used
categories of built-in functions in R:

1. Mathematical Functions

• Arithmetic: abs(x), sqrt(x), ceiling(x), floor(x), round(x, digits), signif(x, digits)

• Logarithmic and Exponential: log(x), log10(x), exp(x)

• Trigonometric: sin(x), cos(x), tan(x), asin(x), acos(x), atan(x)

2. Statistical Functions

• Summary Statistics: mean(x), median(x), sd(x), var(x), range(x), quantile(x), summary(x)

• Probability Distributions:

o Density: dnorm(x), dbinom(x, size, prob)

o Distribution: pnorm(x), pbinom(x, size, prob)

o Quantile: qnorm(p), qbinom(p, size, prob)

o Random Generation: rnorm(n, mean, sd), rbinom(n, size, prob)

3. Character Functions

• String Manipulation: nchar(x), toupper(x), tolower(x), substring(x, start, stop)

• Pattern Matching: grep(pattern, x), grepl(pattern, x), gsub(pattern, replacement, x),


sub(pattern, replacement, x)

4. Data Manipulation Functions

• Sorting and Ordering: sort(x), order(x), rank(x)

• Aggregation: table(x), aggregate(), tapply(), by()

• Reshaping: reshape(), rbind(), cbind()

• Apply Family: apply(), lapply(), sapply(), tapply(), mapply()

5. Logical and Relational Functions

• Logical Operators: all(x), any(x), isTRUE(x)

• Relational Functions: which(x), which.min(x), which.max(x)


6. Input and Output

• Reading Data: read.table(), read.csv(), scan()

• Writing Data: write.table(), write.csv()

• Printing: print(x), cat(x), message(x)

7. Date and Time Functions

• Current Date/Time: Sys.Date(), Sys.time()

• Conversion: as.Date(), as.POSIXct(), as.POSIXlt()

• Date-Time Calculations: difftime(time1, time2), format(Sys.time(), format)

8. Utility Functions

• Checking Data Type: is.numeric(), is.character(), is.data.frame(), is.matrix()

• Coercion: as.numeric(), as.character(), as.factor(), as.matrix()

• Miscellaneous: length(x), dim(x), str(x), typeof(x), class(x)

Example Usage:

Copy code

# Statistical functions

x <- c(1, 2, 3, 4, 5)

mean(x) #3

sd(x) # 1.581139

# String functions

text <- "Hello, R!"

toupper(text) # "HELLO, R!"

# Apply function

mat <- matrix(1:9, nrow = 3)

apply(mat, 1, sum) # Row sums: 6, 15, 24

Let me know if you'd like further explanations or examples!


4o

You said:

with examples for 20 marks

ChatGPT said:

ChatGPT

Here’s a detailed explanation of R's built-in functions with examples, categorized to cover their usage
comprehensively for a 20-mark answer:

1. Mathematical Functions

R offers mathematical functions for basic to advanced computations.

Examples:

Copy code

# Absolute value and square root

x <- -16

abs(x) # 16

sqrt(16) #4

# Ceiling and floor

num <- 7.8

ceiling(num) # 8

floor(num) # 7

# Exponential and logarithm

exp(1) # 2.718281 (e^1)

log(10) # 2.302585 (Natural log)

2. Statistical Functions

Statistical functions are commonly used for data analysis.

Examples:

r
Copy code

data <- c(12, 15, 20, 25, 30)

# Mean, median, and standard deviation

mean(data) # 20.4

median(data) # 20

sd(data) # 7.483314

# Summary of data

summary(data)

# Min. 1st Qu. Median Mean 3rd Qu. Max.

# 12 15 20 20.4 25 30

3. Character Functions

R provides string manipulation functions for text analysis.

Examples:

Copy code

text <- "R Programming"

# String length

nchar(text) # 13

# Convert to uppercase and substring

toupper(text) # "R PROGRAMMING"

substring(text, 1, 3) # "R P"

4. Data Manipulation Functions

These functions are used for organizing and reshaping data.

Examples:

r
Copy code

# Sorting and ranking

nums <- c(8, 2, 9, 5)

sort(nums) # 2, 5, 8, 9

rank(nums) # 3, 1, 4, 2

# Combining data

vec1 <- c(1, 2)

vec2 <- c(3, 4)

cbind(vec1, vec2) # Combine into columns

rbind(vec1, vec2) # Combine into rows

5. Logical and Relational Functions

These functions test conditions or identify elements in data.

Examples:

Copy code

vec <- c(3, 7, 1, 9)

# Logical checks

any(vec > 8) # TRUE

all(vec < 10) # TRUE

# Which function

which(vec > 5) # 2, 4 (Indices where condition is TRUE)

6. Apply Family Functions

R’s apply family is powerful for iterating over data structures.

Examples:

Copy code
matrix_data <- matrix(1:9, nrow = 3)

# Row and column-wise operations

apply(matrix_data, 1, sum) # Row sums: 6, 15, 24

apply(matrix_data, 2, mean) # Column means: 2, 5, 8

7. Probability and Random Numbers

Probability functions are used in simulations and analysis.

Examples:

Copy code

# Normal distribution

rnorm(5, mean = 0, sd = 1) # Generate 5 random numbers

# Probability and quantile

pnorm(1.96) # 0.975 (Cumulative probability for Z=1.96)

qnorm(0.975) # 1.96 (Z-score for 97.5% cumulative)

8. Date and Time Functions

R provides tools for handling date and time data.

Examples:

Copy code

# Current date and time

Sys.Date() # "2024-12-01"

Sys.time() # "2024-12-01 12:34:56"

# Date arithmetic

as.Date("2024-12-10") - Sys.Date() # Time difference in days

9. Input/Output Functions
These functions help in reading and writing data files.

Examples:

Copy code

# Writing and reading data

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

write.csv(data, "data.csv")

read_data <- read.csv("data.csv")

print(read_data)

10. Utility Functions

Utility functions help check and manipulate data types.

Examples:

Copy code

x <- c(1, 2, 3)

# Check data type

is.numeric(x) # TRUE

# Convert data type

y <- as.character(x)

is.character(y) # TRUE

# Length of a vector

length(x) #3

Comprehensive Example

Copy code

# Data creation and manipulation


data <- c(5.5, 7.2, 8.9, 6.4, 7.5)

# Statistical summary

mean(data) # 7.1

summary(data) # Min, Median, Mean, Max, etc.

# Logical operation

any(data > 8) # TRUE

# String operation

text <- "Analysis"

toupper(text) # "ANALYSIS"

# Apply function on matrix

mat <- matrix(1:6, nrow = 2)

apply(mat, 1, sum) # Row sums

This detailed explanation and examples showcase the versatility of R’s built-in functions, making
them invaluable for diverse data analysis tasks.

You might also like