0% found this document useful (0 votes)
8 views7 pages

Codes - Part 1

The document provides an overview of R programming, focusing on data structures such as vectors, factors, lists, and data frames. It includes examples of accessing and manipulating these structures, as well as importing and exporting data from various sources like Excel and SPSS. Additionally, it covers essential functions for data analysis and handling missing values.

Uploaded by

letadereje0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views7 pages

Codes - Part 1

The document provides an overview of R programming, focusing on data structures such as vectors, factors, lists, and data frames. It includes examples of accessing and manipulating these structures, as well as importing and exporting data from various sources like Excel and SPSS. Additionally, it covers essential functions for data analysis and handling missing values.

Uploaded by

letadereje0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Codes

ts

# Thu Dec 26 09:47:49 2024 ------------------------------

#This is helpful for version control and keeping track of your work

x=5+5

y=3

z=x+y

#Connecting R to external databases is essential for several reasons:

#Accessing Large Datasets:

#Working with Real-Time Data:e.g fraud detection

#Collaboration and Data Sharing:

getwd()

setwd("C:/Users/EG/Desktop/R Practice")

getwd()

ls()

rm(X)

rm(list=c("x","y"))

#HELP

help() #general help function in R

help("mean") # help on the specific function

?mean # shortcut for help("mean")

help.search("linear")

example("mean")# examples of how to use a specific function

apropos("mean")

apropos("mean", mode="function")

1
## DATA STRUCTURE

# VECTORS

#C() used to combine elements into vector

Logical <- c(TRUE, FALSE)

absent <- c(1, 3, 6, 9)

weight <- c(1.8, 2, 3.7)

Abebe <- c("He", "is", "a", "statistician")

# mode and [ ] to extract elements of a vector.

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

CConduct <- c("A", "B", "B", "A")

#is.atomic(x) tests if an object x is actually a vector.

#(1)

is.atomic(CConduct)

class(CConduct)

2
cc<-factor(CConduct)

as.numeric(cc)

is.ordered(CConduct)

#(2)

performane <-c("Good", "Very good", "Excellent", "Good")

pfn<-factor(performane, ordered = TRUE, levels = c("Good", "Very good", "Excellent"))

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)

factor(Sex,levels = c(0,1), labels=c("M","F"))

# LIST: it may contain vectors, matrices, data frames, and even other lists.

# List are the most complex of the R objects.

L <-list(1:3, "a", c(TRUE, FALSE, TRUE), c(2.3, 5.9))

str(L)

# To access elements of a list using double square brackets [[ ]]

L[[1]]

L[[4]]

# unlist() to turn a list into an atomic vector

ul<-unlist(L)

str(ul)

# atomic vector is a one-dimensional sequence of elements of the same data type

# DATA FRAMEs: is a list of equal-length vectors.

# row = individual observation and column = variables == Table

# e.g. mydata < − data.frame(col1, col2, col3,...)

3
df<-data.frame(id=1:3,grade=c("A","B","C"))

df

dim(df)

# cbind() used to combine two or more data frames or vectors by column

# 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")))

## OR df$gender<-c("f", "m", "m")

rbind(df, data.frame(id=4, grade="B"))

rbind(dfG, data.frame(id=4, grade="B", gender="f"))

# 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...)

# it is a data wh contain info. on 150 iris flowers.

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)

# Data entry/ Import to R directly

infant_data<-data.frame(Age=numeric(0),gender=character(0),weight=numeric(0))

mydata<-edit(infant_data)

mydata

# to edit or enter additional data

edit(mydata)

## embeding data

#(1)

mydatatxt <-"age gender weight

25 m 166

30 f 115"

mydata1<-read.table(header = TRUE, text = mydatatxt )

#(2)

studentgrades<- "StudentID, First, Last, Math, Science, Social

011, Bob, Smith, 90, 80, 67

012, Jane, Weary, 75, , 80 "

grade <-read.table(header = TRUE, text = studentgrades )

# DETECTION OF MISSING VALUES

airquality[!complete.cases(airquality),] # show each row with NA

any(is.na(airquality$Ozone)) # whether there is a missing under the column or not

5
is.na(airquality) # show each row

colSums(is.na(airquality)) # sum of missing observation under each column

# importing Data from excel, spss and stata

install.packages("xlsx")

library(xlsx)

install.packages("haven")

install.packages("foriegn")

library(haven)

library(foreign)

# stata

practice <- read_dta("practice.dta")

View(practice)

#SPSS

Rain <- read_sav("Rain.sav")

View(Rain)

install.packages("Hmisc")

library(Hmisc)

Rain1 <- spss.get("Rain.sav",use.value.labels=TRUE)

#EXCEL

EastHar <- read_excel("EastHar.xlsx")

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

write.table(df, file = "exported1.txt", col.names=TRUE, row.names=FALSE, sep = "")

write.table(df, file = "exported2.csv", col.names=TRUE, row.names=FALSE, sep = ",")

You might also like