X <- c(3, 2, 4) and Y <- c(1, 2).
What will be output of vector Z that is defined as Z <- X*Y.
X <- c(3, 2, 4)
Y<-c(1,2)
Z<-X*Y
## Warning in X * Y: longer object length is not a multiple of shorter object
## length
Z
## [1] 3 4 4
a = (5, 10, 15, 20, …, 160) b = (87, 86, 85, …, 56)
Use vector arithmetic to multiply these vectors and call the result ‘d’. Select
subsets of d to identify the following.
What are the 19th, 20th, and 21st elements of d?
What are all of the elements of d which are less than 2000?
How many elements of d are greater than 6000?
a<-seq(from=5 ,to=160, by=5) # Create a vector a
print(a)
## [1] 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85
## [18] 90 95 100 105 110 115 120 125 130 135 140 145 150 155 160
length(a)
## [1] 32
b<-seq(from=87,to=56,by=-1)# Create a vector b
print(b)
## [1] 87 86 85 84 83 82 81 80 79 78 77 76 75 74 73 72 71 70 69 68 67 66 65
## [24] 64 63 62 61 60 59 58 57 56
length(b)
## [1] 32
d<-a*b # Use vector arithmetic to multiply these vectors and call the result ‘d’.
print(d)
## [1] 435 860 1275 1680 2075 2460 2835 3200 3555 3900 4235 4560 4875 5180
## [15] 5475 5760 6035 6300 6555 6800 7035 7260 7475 7680 7875 8060 8235 8400
## [29] 8555 8700 8835 8960
d[19:21] #What are the 19th, 20th, and 21st elements of d?
## [1] 6555 6800 7035
j<-d<2000 #What are all of the elements of d which are less than 2000?
d[j]
## [1] 435 860 1275 1680
k<-d>6000 # How many elements of d are greater than 6000?
length(d[k])
## [1] 16
a<-c("Durga Rao","Sai baba","Revanth","Bhargav Kiran") # Create a vector with some of your friend's names
length(a) # Get the first two friends from above vector
## [1] 4
a[1:2] # Get the first two friends from above vector
## [1] "Durga Rao" "Sai baba"
a[2:3] # Get the 2nd and 3rd friends
## [1] "Sai baba" "Revanth"
sort(a) # Sorting your friends by names using 1 method
## [1] "Bhargav Kiran" "Durga Rao" "Revanth" "Sai baba"
Sorting your friends using another method
Order returns the position (index) of the original value and is in the order of sorted sequence, that is smallest value to largest value
a[order(a)]
## [1] "Bhargav Kiran" "Durga Rao" "Revanth" "Sai baba"
# Create a dataframe a.
x1<-c("Ramu","Raju")
y1<-c(50000,60000)
a<-data.frame(name=x1,salary=y1,stringsAsFactors =FALSE)
a
## name salary
## 1 Ramu 50000
## 2 Raju 60000
# Create a dataframe b.
x2<-c("Ramu","Raju")
y2<-c("Associate professor","Assistant professor")
b<-data.frame(name=x2,salary=y2,stringsAsFactors =FALSE)
b
## name salary
## 1 Ramu Associate professor
## 2 Raju Assistant professor
# Merge the two dataframes
merge(a,b,by.x ="name",by.y = "name" )
## name salary.x salary.y
## 1 Raju 60000 Assistant professor
## 2 Ramu 50000 Associate professor
x<-c(1,2,3) # Create a vector x
y<-c(4,5,6) # Create a vector y
z<-c(7,8,9) # Create a vector z
A<-cbind(x,y,z) # Create a matrix A with three vectors
class(A) # check the class of A.
## [1] "matrix"
rownames(A)<-c("a","b","c") # Change the rownames of A
A
## x y z
## a 1 4 7
## b 2 5 8
## c 3 6 9
A<-c(5, 2, -2, 6,7,10,12,14,15) # Create a vector
Y<-A[A>6]
Y
## [1] 7 10 12 14 15
x1<-rnorm(100) # random normal vectors of length 100
x2<-rnorm(100) # random normal vectors of length 100
x3<-rnorm(100) # random normal vectors of length 100
t<-data.frame(a=x1,b=x1+x2,c=x1+x2+x3) # Created a dataframe
plot(t) # Applying plot on it
#sd(t) # Applying standard deviation on it. you will get an error.
# Error in is.data.frame(x) : (list) object cannot be coerced to type 'double'
plot(rnorm(100))
a<-(rnorm(100))
# mean(sqrt(a))# some negative numbers may be generated so square root cannot be calculated. Instead
mean(sqrt(abs(a)))
## [1] 0.8019298
A<-c(1,'a',2,'b')
class(A)
## [1] "character"
((2014-2010)/(2014-1987))*100
## [1] 14.81481
A<-c(4,5,8,11) # Combining into a vector
sum(A) # USing function sum on it.
## [1] 28
class(iris) # Find the class of ‘iris’ dataframe
## [1] "data.frame"
class(iris$Sepal.Length) # class of Sepal.Length
## [1] "numeric"
class(iris$Sepal.Width)# class of Sepal.Width
## [1] "numeric"
class(iris$Petal.Length)# class of Petal.Length
## [1] "numeric"
class(iris$Petal.Width)# class of Petal.Width
## [1] "numeric"
class(iris$Species)# class of Species
## [1] "factor"
summary(iris) # get the summary of ‘iris’
## Sepal.Length Sepal.Width Petal.Length Petal.Width
## Min. :4.300 Min. :2.000 Min. :1.000 Min. :0.100
## 1st Qu.:5.100 1st Qu.:2.800 1st Qu.:1.600 1st Qu.:0.300
## Median :5.800 Median :3.000 Median :4.350 Median :1.300
## Mean :5.843 Mean :3.057 Mean :3.758 Mean :1.199
## 3rd Qu.:6.400 3rd Qu.:3.300 3rd Qu.:5.100 3rd Qu.:1.800
## Max. :7.900 Max. :4.400 Max. :6.900 Max. :2.500
## Species
## setosa :50
## versicolor:50
## virginica :50
##
##
##
head(iris) # get the top 6 rows: by default head displays top 6 six rows
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 5.1 3.5 1.4 0.2 setosa
## 2 4.9 3.0 1.4 0.2 setosa
## 3 4.7 3.2 1.3 0.2 setosa
## 4 4.6 3.1 1.5 0.2 setosa
## 5 5.0 3.6 1.4 0.2 setosa
## 6 5.4 3.9 1.7 0.4 setosa
rownames(iris) # get row names
## [1] "1" "2" "3" "4" "5" "6" "7" "8" "9" "10" "11"
## [12] "12" "13" "14" "15" "16" "17" "18" "19" "20" "21" "22"
## [23] "23" "24" "25" "26" "27" "28" "29" "30" "31" "32" "33"
## [34] "34" "35" "36" "37" "38" "39" "40" "41" "42" "43" "44"
## [45] "45" "46" "47" "48" "49" "50" "51" "52" "53" "54" "55"
## [56] "56" "57" "58" "59" "60" "61" "62" "63" "64" "65" "66"
## [67] "67" "68" "69" "70" "71" "72" "73" "74" "75" "76" "77"
## [78] "78" "79" "80" "81" "82" "83" "84" "85" "86" "87" "88"
## [89] "89" "90" "91" "92" "93" "94" "95" "96" "97" "98" "99"
## [100] "100" "101" "102" "103" "104" "105" "106" "107" "108" "109" "110"
## [111] "111" "112" "113" "114" "115" "116" "117" "118" "119" "120" "121"
## [122] "122" "123" "124" "125" "126" "127" "128" "129" "130" "131" "132"
## [133] "133" "134" "135" "136" "137" "138" "139" "140" "141" "142" "143"
## [144] "144" "145" "146" "147" "148" "149" "150"
colnames(iris)# get column names
## [1] "Sepal.Length" "Sepal.Width" "Petal.Length" "Petal.Width"
## [5] "Species"
nrow(iris) # get number of rows.
## [1] 150
ncol(iris)# get number of columns
## [1] 5
# Get the last 2 rows in last 2 columns from iris dataset
e<-tail(iris,2) # At first get the last 2 rows using tail function. (Note: by default, tail returns last 6 rows, we have changed it to 2 )
e[,c(4,5)] # returns the dataframe with last 2 columns
## Petal.Width Species
## 149 2.3 virginica
## 150 1.8 virginica
iris[which(iris$Sepal.Width>3.5),] # Get rows with Sepal.Width > 3.5 using which() from iris
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 5 5.0 3.6 1.4 0.2 setosa
## 6 5.4 3.9 1.7 0.4 setosa
## 11 5.4 3.7 1.5 0.2 setosa
## 15 5.8 4.0 1.2 0.2 setosa
## 16 5.7 4.4 1.5 0.4 setosa
## 17 5.4 3.9 1.3 0.4 setosa
## 19 5.7 3.8 1.7 0.3 setosa
## 20 5.1 3.8 1.5 0.3 setosa
## 22 5.1 3.7 1.5 0.4 setosa
## 23 4.6 3.6 1.0 0.2 setosa
## 33 5.2 4.1 1.5 0.1 setosa
## 34 5.5 4.2 1.4 0.2 setosa
## 38 4.9 3.6 1.4 0.1 setosa
## 45 5.1 3.8 1.9 0.4 setosa
## 47 5.1 3.8 1.6 0.2 setosa
## 49 5.3 3.7 1.5 0.2 setosa
## 110 7.2 3.6 6.1 2.5 virginica
## 118 7.7 3.8 6.7 2.2 virginica
## 132 7.9 3.8 6.4 2.0 virginica