It will store a value in a variable.
a<-2
a
## [1] 2
R functions are invoked by its name
c(1,2,3)
## [1] 1 2 3
Tools –> Install Packages —> Select Package name
ctrl+Alt+I iNSERT CHUNCK
cTRL+R for executing the chunk
x<-c(1:20)
x
## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
>install.packages("ggplot2")
library(datasets)
help(c)
## starting httpd help server ... done
x<-c(1,2,3,5:10,NA)
help(sd)
args(sd)
## function (x, na.rm = FALSE)
## NULL
sd(x)
## [1] NA
sd(x,na.rm = TRUE)
## [1] 3.162278
sd(x,TRUE)
## [1] 3.162278
sd(TRUE,x)
## Warning in if (na.rm) "na.or.complete" else "everything": the condition has
## length > 1 and only the first element will be used
## [1] NA
sd(na.rm = TRUE,x)
## [1] 3.162278
sd(TRUE,x=x)
## [1] 3.162278
r-help@r-project.org github statckoverflow
x<-c(4,7,3,2,5)
x
## [1] 4 7 3 2 5
y<-x+2
y
## [1] 6 9 5 4 7
mean(y)
## [1] 6.2
sd(y)
## [1] 1.923538
z<-c(2,3)
x + z
## Warning in x + z: longer object length is not a multiple of shorter object
## length
## [1] 6 10 5 5 7
character numeric integer complex logical
x<-1
class(x)
## [1] "numeric"
x<-1L
class(x)
## [1] "integer"
?class
names ,dimnames dimensions class length
x<-c(1:20)
length(x)
## [1] 20
x<-c(1:20,"shyama")
length(x)
## [1] 21
x
## [1] "1" "2" "3" "4" "5" "6" "7"
## [8] "8" "9" "10" "11" "12" "13" "14"
## [15] "15" "16" "17" "18" "19" "20" "shyama"
x<-c(1:20)
x<-as.character(x)
class(x)
## [1] "character"
x<-c(1:20)
x<-as.factor(x)
class(x)
## [1] "factor"
x<-1
print(x) #explicit printing
## [1] 1
c()function can be used to create vectors of objects
x<- c(0.5, 0.6) #NUMERIC
x<- c(TRUE, FALSE) #logical
x<- c(T,F) #logical
x<- c("a","b","c")
c(TRUE,2)
## [1] 1 2
c(TRUE,2,FALSE,"shyama")
## [1] "TRUE" "2" "FALSE" "shyama"
x<-1:6.0
class(x)
## [1] "integer"
as.numeric(x)
## [1] 1 2 3 4 5 6
x<-c(1,2,3)
class(x)
## [1] "numeric"
x<-c("a","b","c")
class(x)
## [1] "character"
as.numeric(x)
## Warning: NAs introduced by coercion
## [1] NA NA NA
matrix(1:20,nrow = 2)
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 1 3 5 7 9 11 13 15 17 19
## [2,] 2 4 6 8 10 12 14 16 18 20
args(matrix)
## function (data = NA, nrow = 1, ncol = 1, byrow = FALSE, dimnames = NULL)
## NULL
matrix(1:20,nrow = 2,byrow = TRUE)
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## [1,] 1 2 3 4 5 6 7 8 9 10
## [2,] 11 12 13 14 15 16 17 18 19 20
?dim
x<-1:10
dim(x)<-c(2,5)
x
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 3 5 7 9
## [2,] 2 4 6 8 10
y<-20:30
x<-1:10
y<-20:29
z<-50:59
x
## [1] 1 2 3 4 5 6 7 8 9 10
y
## [1] 20 21 22 23 24 25 26 27 28 29
z
## [1] 50 51 52 53 54 55 56 57 58 59
cbind(x,y)
## x y
## [1,] 1 20
## [2,] 2 21
## [3,] 3 22
## [4,] 4 23
## [5,] 5 24
## [6,] 6 25
## [7,] 7 26
## [8,] 8 27
## [9,] 9 28
## [10,] 10 29
rbind(x,y,z)
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
## x 1 2 3 4 5 6 7 8 9 10
## y 20 21 22 23 24 25 26 27 28 29
## z 50 51 52 53 54 55 56 57 58 59
x<-c(1,3,5)
y<-c(2,3,4,5)
x
## [1] 1 3 5
y
## [1] 2 3 4 5
cbind(x,y)
## Warning in cbind(x, y): number of rows of result is not a multiple of
## vector length (arg 1)
## x y
## [1,] 1 2
## [2,] 3 3
## [3,] 5 4
## [4,] 1 5
x<-list(c(1,2,3),c("shyama","manasa"))
x
## [[1]]
## [1] 1 2 3
##
## [[2]]
## [1] "shyama" "manasa"
x[1]
## [[1]]
## [1] 1 2 3
x[2]
## [[1]]
## [1] "shyama" "manasa"
x[1][2]
## [[1]]
## NULL
x[[c(1,2)]]
## [1] 2
x[[c(1,2)]]
## [1] 2
x[[c(2,1)]]
## [1] "shyama"
x<-factor(c("yes","yes","no","yes"))
x
## [1] yes yes no yes
## Levels: no yes
class(x)
## [1] "factor"
x<-factor(c("yes","yes","no","yes"))
class(x)
## [1] "factor"
x<-factor(x)
class(x)
## [1] "factor"
x
## [1] yes yes no yes
## Levels: no yes
args(factor)
## function (x = character(), levels, labels = levels, exclude = NA,
## ordered = is.ordered(x), nmax = NA)
## NULL
table(y)
## y
## 2 3 4 5
## 1 1 1 1
y
## [1] 2 3 4 5
x<-list(c("Rekha","shyama"),c(2,3,5,19,2,7,5,5))
x
## [[1]]
## [1] "Rekha" "shyama"
##
## [[2]]
## [1] 2 3 5 19 2 7 5 5
class(x[[2]])
## [1] "numeric"
x[[c(2,4)]]
## [1] 19
x
## [[1]]
## [1] "Rekha" "shyama"
##
## [[2]]
## [1] 2 3 5 19 2 7 5 5
y<-as.factor(x[[1]])
y
## [1] Rekha shyama
## Levels: Rekha shyama
table(x[[2]])
##
## 2 3 5 7 19
## 2 1 3 1 1
x
## [[1]]
## [1] "Rekha" "shyama"
##
## [[2]]
## [1] 2 3 5 19 2 7 5 5
is.na() #either there is na or not is.nan() #either there is nan or not NA NaN
x<-c(1,2,3,NA,1,NaN)
x
## [1] 1 2 3 NA 1 NaN
is.na(x)
## [1] FALSE FALSE FALSE TRUE FALSE TRUE
is.nan(x)
## [1] FALSE FALSE FALSE FALSE FALSE TRUE
x<-c(1,2,NaN,NA,4)
is.na(x)
## [1] FALSE FALSE TRUE TRUE FALSE
is.nan(x)
## [1] FALSE FALSE TRUE FALSE FALSE
set of frames is Data frames Length of the vertical frames is equal to length of rows
args(data.frame)
## function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE,
## stringsAsFactors = default.stringsAsFactors())
## NULL
x<-data.frame(name=c("shyama","rekha","Athanu","sujith"),age=c(24,23,23,24),sex=c("F","F","M","M"))
x
## name age sex
## 1 shyama 24 F
## 2 rekha 23 F
## 3 Athanu 23 M
## 4 sujith 24 M
class(x)
## [1] "data.frame"
y<-cbind(name=c("shyama","rekha","Athanu","sujith"),age=c(24,23,23,24),sex=c("F","F","M","M"))
y
## name age sex
## [1,] "shyama" "24" "F"
## [2,] "rekha" "23" "F"
## [3,] "Athanu" "23" "M"
## [4,] "sujith" "24" "M"
class(y)
## [1] "matrix"
z=as.data.frame(y)
z
## name age sex
## 1 shyama 24 F
## 2 rekha 23 F
## 3 Athanu 23 M
## 4 sujith 24 M
class(z)
## [1] "data.frame"
x
## name age sex
## 1 shyama 24 F
## 2 rekha 23 F
## 3 Athanu 23 M
## 4 sujith 24 M
str(x)
## 'data.frame': 4 obs. of 3 variables:
## $ name: Factor w/ 4 levels "Athanu","rekha",..: 3 2 1 4
## $ age : num 24 23 23 24
## $ sex : Factor w/ 2 levels "F","M": 1 1 2 2
x[,2]
## [1] 24 23 23 24
x[2,2]
## [1] 23
x[2,]
## name age sex
## 2 rekha 23 F
names(x[,2])
## NULL
as.character(x[,1])
## [1] "shyama" "rekha" "Athanu" "sujith"
class(as.character(x[,1]))
## [1] "character"
str(x)
## 'data.frame': 4 obs. of 3 variables:
## $ name: Factor w/ 4 levels "Athanu","rekha",..: 3 2 1 4
## $ age : num 24 23 23 24
## $ sex : Factor w/ 2 levels "F","M": 1 1 2 2
x[,1]=as.character(x[,1])
str(x)
## 'data.frame': 4 obs. of 3 variables:
## $ name: chr "shyama" "rekha" "Athanu" "sujith"
## $ age : num 24 23 23 24
## $ sex : Factor w/ 2 levels "F","M": 1 1 2 2
x[,2]=as.integer(x[,2])
str(x)
## 'data.frame': 4 obs. of 3 variables:
## $ name: chr "shyama" "rekha" "Athanu" "sujith"
## $ age : int 24 23 23 24
## $ sex : Factor w/ 2 levels "F","M": 1 1 2 2
y
## name age sex
## [1,] "shyama" "24" "F"
## [2,] "rekha" "23" "F"
## [3,] "Athanu" "23" "M"
## [4,] "sujith" "24" "M"
class(y)
## [1] "matrix"
y=as.data.frame(y)
str(y)
## 'data.frame': 4 obs. of 3 variables:
## $ name: Factor w/ 4 levels "Athanu","rekha",..: 3 2 1 4
## $ age : Factor w/ 2 levels "23","24": 2 1 1 2
## $ sex : Factor w/ 2 levels "F","M": 1 1 2 2
y
## name age sex
## 1 shyama 24 F
## 2 rekha 23 F
## 3 Athanu 23 M
## 4 sujith 24 M
y[,2]=as.integer(as.character(y[,2]))
str(y)
## 'data.frame': 4 obs. of 3 variables:
## $ name: Factor w/ 4 levels "Athanu","rekha",..: 3 2 1 4
## $ age : int 24 23 23 24
## $ sex : Factor w/ 2 levels "F","M": 1 1 2 2
y[,1]=as.character(y[,1])
str(y)
## 'data.frame': 4 obs. of 3 variables:
## $ name: chr "shyama" "rekha" "Athanu" "sujith"
## $ age : int 24 23 23 24
## $ sex : Factor w/ 2 levels "F","M": 1 1 2 2
y$age
## [1] 24 23 23 24
str(y$age)
## int [1:4] 24 23 23 24
str(y)
## 'data.frame': 4 obs. of 3 variables:
## $ name: chr "shyama" "rekha" "Athanu" "sujith"
## $ age : int 24 23 23 24
## $ sex : Factor w/ 2 levels "F","M": 1 1 2 2
str(y$name)
## chr [1:4] "shyama" "rekha" "Athanu" "sujith"
y
## name age sex
## 1 shyama 24 F
## 2 rekha 23 F
## 3 Athanu 23 M
## 4 sujith 24 M
names(y)
## [1] "name" "age" "sex"
nrow(y) ## no of rows
## [1] 4
ncol(y) ##no of cols
## [1] 3
y<-data.frame(c("shyama","rekha","Athanu","sujith"),age=c(24,23,23,24),sex=c("F","F","M","M"))
y
## c..shyama....rekha....Athanu....sujith.. age sex
## 1 shyama 24 F
## 2 rekha 23 F
## 3 Athanu 23 M
## 4 sujith 24 M
class(y)
## [1] "data.frame"
colnames(y)<-c("Name","Age","Sex") ##to change the attribute names
y
## Name Age Sex
## 1 shyama 24 F
## 2 rekha 23 F
## 3 Athanu 23 M
## 4 sujith 24 M
str(y)
## 'data.frame': 4 obs. of 3 variables:
## $ Name: Factor w/ 4 levels "Athanu","rekha",..: 3 2 1 4
## $ Age : num 24 23 23 24
## $ Sex : Factor w/ 2 levels "F","M": 1 1 2 2
y[,1]<-as.character(y[,1])
y
## Name Age Sex
## 1 shyama 24 F
## 2 rekha 23 F
## 3 Athanu 23 M
## 4 sujith 24 M
str(y)
## 'data.frame': 4 obs. of 3 variables:
## $ Name: chr "shyama" "rekha" "Athanu" "sujith"
## $ Age : num 24 23 23 24
## $ Sex : Factor w/ 2 levels "F","M": 1 1 2 2
Comments