0% found this document useful (0 votes)
7 views2 pages

R Programming Advanced Topics

The document covers advanced topics in R programming, including the use of factors for categorical data representation and frequency tables. It explains how to access input and output using functions like readline() and read.csv(), as well as basic and advanced graphing techniques in R. Additionally, it introduces the apply family of functions, which provide vectorized alternatives to loops for data manipulation.

Uploaded by

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

R Programming Advanced Topics

The document covers advanced topics in R programming, including the use of factors for categorical data representation and frequency tables. It explains how to access input and output using functions like readline() and read.csv(), as well as basic and advanced graphing techniques in R. Additionally, it introduces the apply family of functions, which provide vectorized alternatives to loops for data manipulation.

Uploaded by

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

Advanced Topics in R Programming

1. Factors and Tables

Factors in R are used to represent categorical data. They store both the values and the
corresponding levels.
Example:
gender <- factor(c("Male", "Female", "Female", "Male", "Female"))
table(gender) shows the frequency count of each category.

Tables are used to generate frequency distributions using the table() function.
Example:
table(gender, age_group)

2. Accessing Input and Output

R allows input from users via readline(), and data from files using read.csv() or read.table().
Example:
name <- readline("Enter your name: ")
data <- read.csv("data.csv")

Output can be printed using print() or cat(), and written to files using write.csv() or
write.table().
Example:
write.csv(data, "output.csv")

3. Graphs in R

R supports extensive visualization tools.

Basic plotting functions:


plot(x, y), hist(), barplot(), boxplot()

Advanced plotting using ggplot2:


ggplot(data = mpg, aes(x = displ, y = hwy)) + geom_point()
These tools are essential for data analysis and pattern recognition.

4. R Apply Family

The apply family of functions are vectorized alternatives to loops.

apply(): Applies functions to rows or columns of matrices.


lapply(): Applies functions to list elements, returns list.
sapply(): Like lapply but returns vector/matrix when possible.
tapply(): Applies functions over subsets defined by a factor.
mapply(): Multivariate apply to multiple arguments.

Examples:
apply(matrix(1:9, nrow=3), 1, sum)
lapply(list(a=1:5, b=6:10), mean)
sapply(list(a=1:5, b=6:10), mean)
tapply(c(1,2,3,4,5,6), factor(c("A","A","B","B","C","C")), sum)
mapply(sum, 1:5, 6:10)

You might also like