Lab2 - mayumispice/R GitHub Wiki

read a file without converting characters to numbers

examAnxiety <- read.csv("examAnxiety.csv",stringsAsFactors = FALSE)

Calling a variable

two ways to do it

examAnxiety$Study.Hours
examAnxiety[,3]

get subset of data

get first 20 rows of first three cols

examAnxiety[1:20,1:3]

Removing data

Removing a col Study.Hours

examAnxiety$Study.Hours <- NULL
examAnxiety <- examAnxiety[,-2]

remove 1-20 rows

examAnxiety <- examAnxiety[-c(1:20),]

Checks for errors and fixing them...

use unique function to display the values

unique(examAnxiety$Sex)

now change "male" to "Male"

examAnxiety$Sex[examAnxiety$Sex=="male"] <- "Male"

create a subset of a dataframe

create a dataframe males

extract data with rows where Sex == Male, all cols

males <- examAnxiety[examAnxiety$Sex=="Male",]

create a dataframe who pass, first add one col called group, and assign 0, then assign 1 to data with Score >= 50, then remove the group col

examAnxiety$group <- 0
examAnxiety$group[examAnxiety$Exam.Score>=50]<-1
pass <- examAnxiety[examAnxiety$group==1,]
pass <- pass [, -6]