Codes - Part 1
Codes - Part 1
ts
#This is helpful for version control and keeping track of your work
x=5+5
y=3
z=x+y
getwd()
setwd("C:/Users/EG/Desktop/R Practice")
getwd()
ls()
rm(X)
rm(list=c("x","y"))
#HELP
help.search("linear")
apropos("mean")
apropos("mean", mode="function")
1
## DATA STRUCTURE
# VECTORS
mode(weight)
absent[3:4]
# Coercion functions in R are functions that change the data type (or "class") of an object.
x<-3.45
class(x)
xxx<-as.integer(x)
class(xxx)
xxx
# FACTORS
#(1)
is.atomic(CConduct)
class(CConduct)
2
cc<-factor(CConduct)
as.numeric(cc)
is.ordered(CConduct)
#(2)
is.ordered(pf)
#(3) : Numeric variables can be coded as factors using the levels and labels options.
Sex <-c(0,1,1,1,0,1,1,0,0)
# LIST: it may contain vectors, matrices, data frames, and even other lists.
str(L)
L[[1]]
L[[4]]
ul<-unlist(L)
str(ul)
3
df<-data.frame(id=1:3,grade=c("A","B","C"))
df
dim(df)
# rbind(): You can add new rows to an existing data frame by using rbind()
cbind(df,data.frame(gender=c("f","m","m")))
dfG<-cbind(df,data.frame(gender=c("f","m","m")))
# To extract/ retrieve columns/ rows of a data frame [r,c] and $ are used
df[2]
df[ ,2]
df$id
df$grade
df[3,2]
df[2, ] #df[2]
#attach() function in R is used to make the variables within a data frame directly accessible in your R
environment without having to explicitly reference the data frame name each time
# Instead of using the $ operator or the [ operator to access columns within data frames
# iris is built in data set (others are ChickWeight , mtcars, airquality, PlantGrowth...)
4
plot(iris$Sepal.Length ~ iris$Petal.Length)
plot(Sepal.Length ~ Petal.Length)
attach(iris)
str(iris)
plot(Sepal.Length ~ Petal.Length)
detach(iris)
plot(Sepal.Length ~ Petal.Length)
infant_data<-data.frame(Age=numeric(0),gender=character(0),weight=numeric(0))
mydata<-edit(infant_data)
mydata
edit(mydata)
## embeding data
#(1)
25 m 166
30 f 115"
#(2)
5
is.na(airquality) # show each row
install.packages("xlsx")
library(xlsx)
install.packages("haven")
install.packages("foriegn")
library(haven)
library(foreign)
# stata
View(practice)
#SPSS
View(Rain)
install.packages("Hmisc")
library(Hmisc)
#EXCEL
View(EastHar)
6
# Import from the nth sheet
library(xlsx)
EastHar<-"C:/Users/EG/Desktop/R Practice/EastHar.xlsx"
EastHar_new<-read_xlsx(EastHar,2)
## Exporting data
df