Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

## course requirement: zeal, google, stackoverflow

###Introduction to R
#practicising common functions

#importing excel data file


library(readxl) #calling "readxl" package which makes working on excel file easy
Concrete_Data <- read_excel("E:/IIM Sambalpur/Term V/BAR/Data/Concrete_Data.xls") #importing
excel file
View(Concrete_Data) #viewing the excel file

conc<-as.data.frame(Concrete_Data) #storing it in dataframe format


##Observation/variable selection
conc_01 <- conc[1:20,] #selecting rows 1 to 20 from data
conc_02 <- conc[,9] #selecting column 9
conc_03 <- conc[1:20,9] #selecting row 1 to 20 & column 9
View(conc_03)
###doubt difference between Data & Value in Global Environment?

conc_1 <- conc[c(10:21),c(2,6:7)] #selecting rows 1 to 20 & column 2, 6 & 7


conc_2 <- conc[-c(1:100,200:530),-c(1,3:5)] #rejecting rows 1 to 100 & 200 to 530 and columns
1,3,4,5 #1030-100-331
#subsetting rows for a given criteria-comp strength and columns to be returned
##important
conc_3 <-subset(conc, conc$`Cement (component 1)(kg in a m^3 mixture)`>=500, #find data in
column1>=500
select = c("Age (day)")) #for the corresponding data in column1 select data in column8

###working from here


concrete <- Concrete_Data #stores excel file?
class(concrete) #identifies type of data? data frame
dim(concrete) #returns dimensions in this case 1030 rows & 9 columns
concrete[,9] #selects column9?
concrete[9,] #selects row9

View(concrete[c(9, 86), 1]) #selects row9 & 86 of column1


View(concrete[9:86, 1]) #selects row9 to 86 of column1
View(concrete[9:86, c(2,5)]) #selects row9 to 86 of column2 & 5

try <- concrete[9:86, c(2,5)] #storing in variable


remove(try) #removing variable

#doubt??
concrete$rowMeans(concrete[c(1,3,7,9,150), ])
#systematic samples
rowMeans(concrete[c(1,1030,5)])
seq(1,100,4)
seq(8,92,5)
names(conc)[1:9]<-c() #doubt??
#renaming columns/variables
concrete<-Concrete_Data
names(concrete)[1:9] <- c("cement_comp","slag_comp","flyash_comp",
"water_ratio","plastic_comp","cagg_comp",
"fagg_comp","duration","comp_strength") #renaming headers

##or use attach


conc_3_2<-subset(concrete, concrete$cement_comp>=500, #find data in column1>=500
select = c("duration","comp_strength")) #for the corresponding data in column1 select
data in column8 & 9
#Based on criteria (multiple values):
conc_4 <- subset(concrete, comp_strength >50 & cement_comp<= 500, #find data in column9>50 &
column1<=500
select=c("duration","fagg_comp")) #for the corresponding data in column1 select data in
column7 & 8
conc_5 <- subset(concrete,comp_strength <=50 & cement_comp<= 500, #find data in column9<=50
& column1<=500
select=c("duration","fagg_comp")) #for the corresponding data in column1 select data in
column7 & 8
#811+189=1000
which(concrete$cement_comp>500) #search row nos of data in column1>=500
length(which(concrete$cement_comp>500)) #30 #no of elements in column1>=500
##selecting all columns through
conc_6 <- subset(concrete, cement_comp>=500 & duration > 25, #find data in column1>=500 &
column8>25
select=water_ratio:fagg_comp) #for the corresponding data in column1 select data in
column4 to 7

# take a random sample of size 50


# sample without replacement do without (set.seed(123))
set.seed(123) #doubt??
mysample <- concrete[sample(1:nrow(concrete), 50, #select 50 elements from all rows
replace=FALSE),] #no replacement
#replace RS Aggarwal example
#sample should be same to get consistent results

set.seed(123)
mysample_index <- sample(1:nrow(concrete), 50,
replace=FALSE) #doubt??

####Computing cloumn/rwo sums; means etc.


colSums(conc_6) #sum of all elements in the columns
colMeans(conc_6) #mean of all elements in the columns
##verify
rowSums(conc_1) #sum of all elements in the rows
rowMeans(conc_1) #mean of all elements in the rows
##verify

##ADding rows/columns
#the variables should be same for rbind while same #observations for cbind
conc_6_M<-colMeans(conc_6) #mean of all elements in the rows
conc_6<-rbind(conc_6,conc_6_M) #adding mean row to original file
conc_comb<-rbind(conc_4,conc_5) #combining data of two files

#####freeing up memory
# remove(conc)
# remove(conc_01)
# remove(conc_02)
# remove(conc_03)
# remove(conc_1)
# remove(conc_2)
# remove(conc_3)
# remove(conc_3_2)
# remove(conc_4)
# remove(conc_5)
# remove(conc_6)
# remove(conc_6_M)
# remove(Concrete_Data)
# remove(concrete)
# remove(Concrete_Data)
# remove(conc_comb)
# remove(mysample)
# remove(mysample_index)

You might also like