0% found this document useful (0 votes)
13 views

R Programming Basics Guide

Uploaded by

JADEN JOSEPH
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

R Programming Basics Guide

Uploaded by

JADEN JOSEPH
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

R Programming Basics Guide

1.List , define and summarize some basic data structures in R with suitable example.

2. How to create a data frame in R with the help of various data structures.

3. How to add and remove columns from a data frame in R?

4. How to create a user-defined function in R? Demonstrate arithmetic operations.

5.Demonstrate c(),append(),merge(),cbind(),rbind(),sort() functions with suitable example

6. Create a data frame from the following 4 vectors and demonstrate the output:
emp_id = c (1:5)
emp_name = c("Rick", "Dan", "Michelle", "Ryan", "Gary")
start_date=c("2012-01-01", "2013-09-23", "2014-11-15", 03-27")
salary = c(60000, 45000, 75000, 84000, 20000)

i. Display structure and summary of the above data frame

ii. Extract the emp_name and salary columns from the above data frame.

iii. Extract the employee details whose salary is less than or equal to 60000.

7.List and explain various functions that allow users to handle data in R workspace with
appropriate examples.

8. Given the vector x=c(1,3,5,6,7,9,88,23,44,12,56) sort the vector in decreasing order and display
sum of elements <25 using suitable function.

This is the question bank given by my ma'am


I have no clue of r programming

Memory updated

No worries, Jaden! Let's break down the questions and I'll explain R programming step by step.

1. Basic Data Structures in R

Vector: A sequence of elements of the same type. Example:

Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 1/5
R

v <- c(1, 2, 3, 4)

List: A collection of elements of different types. Example:

lst <- list(1, "Hello", TRUE)

Matrix: A two-dimensional array where each element has the same type. Example:

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

Data Frame: A table-like structure with rows and columns where each column can have different
types. Example:

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

Factor: A special type for categorical data. Example:

f <- factor(c("Male", "Female", "Male"))

2. Creating a Data Frame in R

You can create a data frame from vectors like this:

emp_id <- c(1, 2, 3)


emp_name <- c("Rick", "Dan", "Michelle")
start_date <- as.Date(c("2012-01-01", "2013-09-23", "2014-11-15"))
salary <- c(60000, 45000, 75000)

df <- data.frame(emp_id, emp_name, start_date, salary)


print(df)

3. Add and Remove Columns from a Data Frame

Add a column:

Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 2/5
R

df$new_column <- c(1, 2, 3)

Remove a column:

df$new_column <- NULL

4. Creating a User-defined Function

Here's how you create a function to perform arithmetic operations:

add <- function(x, y) {


return(x + y)
}
result <- add(5, 3)
print(result)

5. Functions Demonstration

`c()`: Combine elements into a vector.

v <- c(1, 2, 3)

`append()`: Add elements to a vector.

v <- append(v, 4)

`merge()`: Merge data frames.

merged_df <- merge(df1, df2, by="common_column")

`cbind()`: Combine columns.

Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 3/5
R

df <- cbind(df, new_column)

`rbind()`: Combine rows.

df <- rbind(df, new_row)

`sort()`: Sort a vector.

sorted <- sort(v)

6. Data Frame Creation

emp_id <- c(1:5)


emp_name <- c("Rick", "Dan", "Michelle", "Ryan", "Gary")
start_date <- as.Date(c("2012-01-01", "2013-09-23", "2014-11-15", "2014-03-27"))
salary <- c(60000, 45000, 75000, 84000, 20000)

emp_df <- data.frame(emp_id, emp_name, start_date, salary)

i. Display structure and summary:

str(emp_df)
summary(emp_df)

ii. Extract emp_name and salary:

emp_df[, c("emp_name", "salary")]

iii. Employees with salary <= 60000:

Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 4/5
R

emp_df[emp_df$salary <= 60000, ]

7. Functions for Handling Data in R

`ls()`: List all objects in the workspace.

ls()

`rm()`: Remove objects from the workspace.

rm(object_name)

`getwd()`: Get the current working directory.

getwd()

`setwd()`: Set the working directory.

setwd("/path/to/directory")

8. Vector Sorting and Sum Calculation

x <- c(1, 3, 5, 6, 7, 9, 88, 23, 44, 12, 56)


sorted_x <- sort(x, decreasing=TRUE)
sum_x <- sum(x[x < 25])
print(sorted_x)
print(sum_x)

These should help you get started with R programming!

Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 5/5

You might also like