Run button within the chunk or by placing your cursor inside it and pressing Ctrl+Shift+Enter Add a new chunk by clicking the Insert Chunk button on the toolbar or by pressing Ctrl+Alt+I.

  1. Creating and printing data frames
kids <- c("Alex","Liza")
ages <- c(9,7)
ka <- data.frame(kids,ages,stringsAsFactors=FALSE)
ka # matrix-like viewpoint
  1. Exploring DF
ka[[1]]
## [1] "Alex" "Liza"
ka$kids
## [1] "Alex" "Liza"
ka[,1]
## [1] "Alex" "Liza"
str(ka)
## 'data.frame':    2 obs. of  2 variables:
##  $ kids: chr  "Alex" "Liza"
##  $ ages: num  9 7

3.Extract subdata frames by rows or columns

getwd()
## [1] "C:/1rdata"
setwd("C:/1rdata")
examsquiz <- read.csv("examsquiz.csv",sep=",",header=TRUE)
head(examsquiz)
examsquiz[1:3,] #extract rows
examsquiz[1:3,2] #extract values for rows ... in column ... 
## [1] 3.3 3.2 2.0
class(examsquiz[1:3,2])
## [1] "numeric"
examsquiz[2:5,2,drop=FALSE] #keep it as a onecolumn DF (default vector due to the fact that 1 line only)
examsquiz[examsquiz$Exam1 >= 3,] #filtering to extract the subframe
  1. Treatment of NA Values
x <- c(20,400,NA)
mean(x)
## [1] NA
mean(x,na.rm=TRUE)
## [1] 210
  1. Apply subset() function to DF for row selection
examsquiz[examsquiz$Exam1 >= 2,] #way_1 
subset(examsquiz,Exam1 >= 2) #way_2, using 'subset()' function