0% found this document useful (0 votes)
27 views340 pages

Data Analysis Using R-Compressed

The document contains examples demonstrating various R functions for manipulating vectors, matrices, arrays, lists, and data frames. Some key examples include: - Summarizing the elements of a vector using a recursive sum function. - Printing triangle star patterns using nested for loops. - Binding data frames together row-wise and column-wise using rbind and cbind. - Adding, removing, and accessing elements of data frames. - Merging two data frames on a common column using the merge function from the plyr package.

Uploaded by

Vyshnavi Kolluri
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)
27 views340 pages

Data Analysis Using R-Compressed

The document contains examples demonstrating various R functions for manipulating vectors, matrices, arrays, lists, and data frames. Some key examples include: - Summarizing the elements of a vector using a recursive sum function. - Printing triangle star patterns using nested for loops. - Binding data frames together row-wise and column-wise using rbind and cbind. - Adding, removing, and accessing elements of data frames. - Merging two data frames on a common column using the merge function from the plyr package.

Uploaded by

Vyshnavi Kolluri
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/ 340

x<-c(4,3,5,6,11,19)

x[x<5]+x[x<6]
x<-matrix(1:7, nrow=3)
x
repeat_each <- rep(c(1,2,3), each = 3)
repeat_each
repeat_times <- rep(c(1,2,3), times = 3)
repeat_times
repeat_indepent <- rep(c(1,2,3), times = c(5,2,1))
repeat_indepent
v<-seq(from=1,to=3.3,by=0.4)
v
v[c(-1,-2)]
m1 <- matrix(1:6, byrow=TRUE)
m2 <- matrix(1:6, byrow=FALSE)
m1-m2
c <- c(TRUE, 24, FALSE, -34, 0)
c <- as.character(c)
c
thisarray <- c(1:18)
multiarray <- array(thisarray, dim = c(4, 3, 2))
multiarray[2,3,1]
multiarray[2,3,2]
thisarray <- c(1:18)
multiarray <- array(thisarray, dim = c(4, 3, 2))
multiarray[1,,1]
thisarray <- c(1:18)
multiarray <- array(thisarray, dim = c(4, 3, 2))
multiarray[,1,1]
thisarray <- c(1:18)
multiarray <- array(thisarray, dim = c(4, 3, 2))
multiarray[2,3,]
thisarray <- c(1:18)
multiarray <- array(thisarray, dim = c(4, 3, 2))
multiarray[,,1]
thisarray <- c(1:18)
multiarray <- array(thisarray, dim = c(4, 3, 2))
multiarray[,1,]
thisarray <- c(1:18)
multiarray <- array(thisarray, dim = c(4, 3, 2))
multiarray[1,,]
thisarray <- c(1:18)
multiarray <- array(thisarray, dim = c(2, 3, 2, 2))
multiarray[2,3,1,2]
multiarray[2,3,2,2]
thisarray <- c(1:18)
multiarray <- array(thisarray, dim = c(2, 3, 2, 2))
multiarray[2,3,1,]
thisarray <- c(1:18)
multiarray <- array(thisarray, dim = c(2, 3, 2, 2))
multiarray[2,3,,1]
thisarray <- c(1:18)
multiarray <- array(thisarray, dim = c(2, 3, 2, 2))
multiarray[2,3,,]
thisarray <- c(1:18)
multiarray <- array(thisarray, dim = c(2, 3, 2, 2))
multiarray[2,,,]
thisarray <- c(1:18)
multiarray <- array(thisarray, dim = c(2, 3, 2, 2))
multiarray[,3,,]
thisarray <- c(1:18)
multiarray <- array(thisarray, dim = c(2, 3, 2, 2))
multiarray[,,2,]
thisarray <- c(1:18)
multiarray <- array(thisarray, dim = c(2, 3, 2, 2))
multiarray[,,,2]
n = c(2, 3, 5)
s = c("aa", "bb", "cc", "dd", "ee")
b = c(TRUE, FALSE, TRUE, FALSE,FALSE)
lst<-list(n,s,b)
lst
n = c(2, 3, 5)
s = c("aa", "bb", "cc", "dd", "ee")
b = c(TRUE, FALSE, TRUE, FALSE,FALSE)
lst<-list(n,s,b)
lst[2]
class(lst[2])
n = c(2, 3, 5)
s = c("aa", "bb", "cc", "dd", "ee")
b = c(TRUE, FALSE, TRUE, FALSE,FALSE)
lst<-list(n,s,b)
lst[[2]]
class(lst[[2]])
n = c(2, 3, 5)
s = c("aa", "bb", "cc", "dd", "ee")
b = c(TRUE, FALSE, TRUE, FALSE,FALSE)
lst<-list(n,s,b)
lst[[2]][4]
a <- c('ab','de','gh')
b <- c(11,23,35,426)
lst <- list(a,b)
# code to access 35

lst[[2]][3]
x<-c(5,6)
y<-matrix(1:4,nrow=2,ncol=2)
l<- list(x,y)
l[[2]][1,]
thisarray <- c(1:24)
dim(thisarray)
a <- c('ab','de','gh')
b <- c(11,23,35,426)
lst <- list(a,b)
dim(lst)
Nested_function <- function(x, y) {
a <- x + y return(a)
}
Nested_function(Nested_function(2,2), Nested_function(3,3))
my_function <- function(country = "Norway") {
paste("I am from", country)
}
my_function("Sweden")
my_function("India")
my_function()
my_function("USA")
Outer_func <- function(x) {
Inner_func <- function(y) {
a <- x + y
return(a)
}
return (Inner_func)
}
output <- Outer_func(3)
output(5)
mat<-matrix(c(1:9),3,3)
sum<-0
for(i in 1:nrow(mat)){
for(j in 1:ncol(mat)){
sum<-sum+mat[i,j]
}
}
sum
mat<-matrix(c(1:9),3,3)
result<-0
for(i in 1:nrow(mat)){
for(j in 1:ncol(mat)){
if(mat[i,j]>result){
result<-mat[i,j]
}
}
}
result
mat<-matrix(c(1:9),3,3)
result<-0
for(i in 1:nrow(mat)){
for(j in 1:ncol(mat)){
if(i==j){
result<-result+mat[i,j]
}
}
}
result
mat<-matrix(c(1:9),3,3)
result<-0
for(i in 1:nrow(mat)){
for(j in 1:ncol(mat)){
if((mat[i,j]%%2)==0){
result<-result+mat[i,j]
}
}
}
result
result <- function(x){
if(x==0 || x==1)
{
return(1)
}
else
{
return(x*result(x-1))
}
}
result(5)
sum <- function(vec){
if(length(vec)<=1)
{
return(vec[1])
}
else
{
return(vec[1]+sum(vec[-1]))
}
}
v <- c(1:10)
sum(v)
stars = NULL
k<-5
for(i in 1:5){
for(j in 1:k){
stars = c(stars, i)
}
print(stars)
k<-k-1
stars = NULL
}
stars = NULL
for(i in 1:5){
for(j in 1:i){
stars = c(stars, i)
}
print(stars)
stars = NULL
}
stars = NULL
k<-1
for(i in 1:5){
for(j in 1:k){
stars = c(stars, j)
}
print(stars)
k<-k+1
stars = NULL
}
stars = NULL
k<-5
for(i in 1:5){
for(j in 1:k){
stars = c(stars, j)
}
print(stars)
k<-k-1
stars = NULL
}
stars = NULL
for(i in 1:5){
for(j in 1:5){
if(i==j){
stars=c(stars, 1)
}else{
stars=c(stars, 0)
}
}
print(stars)
stars = NULL
}
stars = NULL
for(i in 1:5){
for(j in 1:5){
if(i>=j){
stars=c(stars, 1)
}else{
stars=c(stars, 0)
}
}
print(stars)
stars = NULL
}
stars = NULL
for(i in 1:5){
for(j in 1:5){
if(i<=j){
stars=c(stars, 1)
}else{
stars=c(stars, 0)
}
}
print(stars)
stars = NULL
}
stars = NULL
for(i in 1:5){
for(j in 1:i){
if((i%%2)==0){
stars = c(stars, 1)
}else{
stars = c(stars, 0)
}
}
print(stars)
stars = NULL
}
for(j in 1:0){
print(9)
}
stars = NULL
for(i in 1:5){
j<-1
k<-1
while(j <=(5-i)){
stars=c(stars,9)
j<-j+1
}
for(m in 1:(2*i-1)){
stars=c(stars,i)
}
while(k <=(5-i)){
stars=c(stars,9)
k<-k+1
}
print(stars)
stars= NULL
}
x<-matrix(c(1:7),3,)
y<-matrix(c(1:7),,3,byrow=T)
x+y
x <- c('one','two','three','four')
y <- c(1,2,3,4)
z <- c(4.5,8.9,3.3,4.5)
df <- data.frame(x,y,z)
df[1]
x <- c('one','two','three','four')
y <- c(1,2,3,4)
z <- c(4.5,8.9,3.3,4.5)
df <- data.frame(x,y,z)
class(df[1])
[1] "data.frame"
x <- c('one','two','three','four')
y <- c(1,2,3,4)
z <- c(4.5,8.9,3.3,4.5)
df <- data.frame(x,y,z)
df[[1]]

[1] "one" "two" "three" "four"


x <- c('one','two','three','four')
y <- c(1,2,3,4)
z <- c(4.5,8.9,3.3,4.5)
df <- data.frame(x,y,z)
class(df[[1]])

[1] "character"
x <- c('one','two','three','four')
y <- c(1,2,3,4)
z <- c(4.5,8.9,3.3,4.5)
df <- data.frame(x,y,z)
df$x

[1] "one" "two" "three" "four"


x <- c('one','two','three','four')
y <- c(1,2,3,4)
z <- c(4.5,8.9,3.3,4.5)
df <- data.frame(x,y,z)
class(df$x)

[1] "character"
x <- c('one','two','three','four')
y <- c(1,2,3,4)
z <- c(4.5,8.9,3.3,4.5)
df <- data.frame(x,y,z)
df[c(1,2),]
x <- c('one','two','three','four')
y <- c(1,2,3,4)
z <- c(4.5,8.9,3.3,4.5)
df <- data.frame(x,y,z)
df[,c(1,2)]
df1 = data.frame(StudentId = c(1:4), Subject = c("OS", "DBMS", "Maths", "Java"))
df2 = data.frame(StudentId = c(5:7), Subject = c("C++", "Python", "ANN"))
df3<-rbind(df1,df2)
df3
df <- data.frame(StudentId = c(1:4), Subject = c("OS", "DBMS", "Maths", "Java"))
new_row<-c(5,"C")
df[nrow(df)+1,]<-new_row
df
df <- data.frame(StudentId = c(1:4), Subject = c("OS", "DBMS", "Maths", "Java"))
df$GPA<-c(8.5,9.2,8.9,9.3)
df
df <- data.frame(StudentId = c(1:4), Subject = c("OS", "DBMS", "Maths", "Java"))
df['GPA']<-c(8.5,9.2,8.9,9.3)
df
df <- data.frame(StudentId = c(1:4), Subject = c("OS", "DBMS", "Maths", "Java"))
GPA<-c(8.5,9.2,8.9,9.3)
df1<-cbind(df,GPA)
id<-c(1,2,3,4,5)
prod=c("F", "H", "B", "S", "D")
units=c(12,19,44, 26,43)
df<-data.frame(id, prod, units)
is.factor(df$prod)
id<-c(1,2,3,4,5)
prod=factor(c("F", "H", "B", "S", "D"))
units=c(12,19,44, 26,43)
df<-data.frame(id, prod, units)
is.factor(df$prod)
df arrange(df,speed,dist) arrange(df,desc(speed),desc(dist)) arrange(df,desc(speed),dist)
df df[order(df$s,df$d),] df[order(-df$s,-df$d),] df[order(-df$s,df$d),]
library(plyr)
df1 = data.frame(StudentId = c(1:4), Subject = c("OS", "DBMS", "Maths", "Java"))
df2 = data.frame(StudentId = c(3:6), City = c("Delhi", "Mumbai", "Pune", "Surat"))
df = merge(x=df1, y=df2, by = "StudentId")
df
library(plyr)
df1 = data.frame(StudentId = c(1:4), Subject = c("OS", "DBMS", "Maths", "Java"))
df2 = data.frame(StudentId = c(3:6), City = c("Delhi", "Mumbai", "Pune", "Surat"))
df = merge(x=df1, y=df2, by = "StudentId", all.x = TRUE)
df
library(plyr)
df1 = data.frame(StudentId = c(1:4), Subject = c("OS", "DBMS", "Maths", "Java"))
df2 = data.frame(StudentId = c(3:6), City = c("Delhi", "Mumbai", "Pune", "Surat"))
df = merge(x=df1, y=df2, by = "StudentId", all.y = TRUE)
df
library(plyr)
df1 = data.frame(StudentId = c(1:4), Subject = c("OS", "DBMS", "Maths", "Java"))
df2 = data.frame(StudentId = c(3:6), City = c("Delhi", "Mumbai", "Pune", "Surat"))
df = merge(x=df1, y=df2, by = "StudentId", all= TRUE)
df
library(plyr)
df1 = data.frame(StudentId = c(1:4), Subject = c("OS", "DBMS", "Maths", "Java"))
df2 = data.frame(StudentId = c(3:6), City = c("Delhi", "Mumbai", "Pune", "Surat"))
df = merge(x=df1, y=df2, by = NULL)
df
library(dplyr)
df1 = data.frame(StudentId = c(1:4), Subject = c("OS", "DBMS", "Maths", "Java"))
df2 = data.frame(StudentId = c(3:6), City = c("Delhi", "Mumbai", "Pune", "Surat"))
df = anti_join(x=df1, y=df2, by = "StudentId")
df
Inner Join
Left Join
Right Join
Full Join

You might also like