R program to add two vectors of integers type and length 3.

#vector sum
x=c(1,2,3)
y=c(8,9,10)
k=x+y
print(k)
## [1]  9 11 13

R program to find the Sum, Mean, and Product of a Vector.

v=c(10,20,30,40)
s=sum(v)
print(s)
## [1] 100
m=mean(v)
print(m)
## [1] 25
p=prod(v)
print(p)
## [1] 240000

R program to find the minimum and the maximum of a Vector.

v=c(1,2,3,4)
print(max(v))
## [1] 4
print(min(v))
## [1] 1

R program to create a list containing a vector, a matrix, and a list and giving names to the elements in the list. Accessing the first and second elements of the list

v=c(1,2,3,4)
m=matrix(seq(1,9),nrow = 3,ncol = 3,byrow = TRUE)
l=list(7,8,9,5)
lnames=c("newVector","newMatrix","newList")
l2=list(v,m,l)
names(l2)=lnames
print(l2)
## $newVector
## [1] 1 2 3 4
## 
## $newMatrix
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]    4    5    6
## [3,]    7    8    9
## 
## $newList
## $newList[[1]]
## [1] 7
## 
## $newList[[2]]
## [1] 8
## 
## $newList[[3]]
## [1] 9
## 
## $newList[[4]]
## [1] 5
#accessing first and second element of the list
print(l2[1:2])
## $newVector
## [1] 1 2 3 4
## 
## $newMatrix
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]    4    5    6
## [3,]    7    8    9

R program to create a matrix of size 3x5.

v=c(1:15)
m=matrix(v,nrow = 3,ncol = 5,byrow = TRUE)
print(m)
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    1    2    3    4    5
## [2,]    6    7    8    9   10
## [3,]   11   12   13   14   15

R program to access the element at 3rd column and 2nd row, only the 3rd row and only the 4th column of a given matrix.

print(m[2,3])
## [1] 8
print(m[3,4])
## [1] 14

Creating a DataFrame from given vectors and display in R

Name = c("Ram","John","Priya","Jahnavi")
Marks = c(100,98,100,98)
Age = c(19,18,19,18)
data=data.frame(Name,Marks,Age)
print(data)
##      Name Marks Age
## 1     Ram   100  19
## 2    John    98  18
## 3   Priya   100  19
## 4 Jahnavi    98  18

Inserting multiple rows in R DataFrame

d2=data.frame(
  Name=c("John","Smith"),
  Marks=c(67,47),
  Age=c(24,23)
)

data=rbind(data,d2)
print(data)
##      Name Marks Age
## 1     Ram   100  19
## 2    John    98  18
## 3   Priya   100  19
## 4 Jahnavi    98  18
## 5    John    67  24
## 6   Smith    47  23

Adding column to dataframe in R

IQ=c(200,100,200,100,190,187)
data=cbind(data,IQ)
print(data)
##      Name Marks Age  IQ
## 1     Ram   100  19 200
## 2    John    98  18 100
## 3   Priya   100  19 200
## 4 Jahnavi    98  18 100
## 5    John    67  24 190
## 6   Smith    47  23 187

Extracting first N rows from dataframe in R

print(head(data,3))
##    Name Marks Age  IQ
## 1   Ram   100  19 200
## 2  John    98  18 100
## 3 Priya   100  19 200

Sort DataFrame by column name in R

print(data[order(names(data))])
##   Age  IQ Marks    Name
## 1  19 200   100     Ram
## 2  18 100    98    John
## 3  19 200   100   Priya
## 4  18 100    98 Jahnavi
## 5  24 190    67    John
## 6  23 187    47   Smith

merge two DataFrames in R

d22=data.frame(
  Rollno=c(1,2,3,4,5,6),
  Grade=c('A','B','B','A','A','B'),
  Name=c("Ram","John","Priya","Jahnavi","John","Smith")
)
data=merge(data,d22)
print(data)
##      Name Marks Age  IQ Rollno Grade
## 1 Jahnavi    98  18 100      4     A
## 2    John    98  18 100      2     B
## 3    John    98  18 100      5     A
## 4    John    67  24 190      2     B
## 5    John    67  24 190      5     A
## 6   Priya   100  19 200      3     B
## 7     Ram   100  19 200      1     A
## 8   Smith    47  23 187      6     B

Append one dataframe to the end of another dataframe in R

d2=data.frame(
  Name=c("Surya","Mahitha"),
  Marks=c(87,77),
  Age=c(29,27),
  IQ=c(197,154),
  Rollno=c(9,56),
  Grade=c('A','B')
)

print(rbind(data,d2))
##       Name Marks Age  IQ Rollno Grade
## 1  Jahnavi    98  18 100      4     A
## 2     John    98  18 100      2     B
## 3     John    98  18 100      5     A
## 4     John    67  24 190      2     B
## 5     John    67  24 190      5     A
## 6    Priya   100  19 200      3     B
## 7      Ram   100  19 200      1     A
## 8    Smith    47  23 187      6     B
## 9    Surya    87  29 197      9     A
## 10 Mahitha    77  27 154     56     B