setwd("C:/SE")
#working with vectors
#characters:
vChr<-c("One","Two","Three","Four")
#numeric vector
vNum<-c(1,2,3,4)
#creating a data frame
df<-data.frame(vChr,vNum)
head(df)
## vChr vNum
## 1 One 1
## 2 Two 2
## 3 Three 3
## 4 Four 4
#selecting rows and columns.
#use squared brackets to select x(rows) and y(columns) as in [x,y]
#select first two rows of df and all columns (a space after the comma means "select all")
df2<-df[1:2,]
#select first three rows and first column:
df3.1<-df[1:3,1]
#subsetting numerics using mathmatical operators
vSub<-subset(df,df$vNum<2)
vSub
## vChr vNum
## 1 One 1
#subset using "less than or equal to"
vSub1<-subset(df,df$vNum <=2)
vSub1
## vChr vNum
## 1 One 1
## 2 Two 2
#subsetting with characters
vSub2<-subset(df,df$vChr=="One")
vSub2
## vChr vNum
## 1 One 1
#subsetting with != ("is not")
vSub3<-subset(df,df$vChr!="Four")
vSub3
## vChr vNum
## 1 One 1
## 2 Two 2
## 3 Three 3
#using boolean operators "&"(AND) and "|" (OR)
ANDsub<-subset(df,df$vChr=="One" & df$vNum==1) #AND
ANDsub
## vChr vNum
## 1 One 1
ORsub<-subset(df,df$vNum==1 | df$vNum==2) #OR
ORsub
## vChr vNum
## 1 One 1
## 2 Two 2
#saving your workspace to file using the save.image() function - this allows you to load in your results when you start your next R session
save.image(file = "test.RData")
#loading back in
load("test.RData")