Exercise 5: Data Frame

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Exercise 5: Data frame

#A data frame is like a matrix, but here each column may have different mode.
#Technically, a data frame is a list, with the components of that list being
#equal-length vectors

#block1
library(data.table)
kids <- c("Manish", "Abhay")
ages <- c(12, 10)
d <- data.frame(kids, ages)
d
str(d)

d <- data.frame(kids, ages, stringsAsFactors = FALSE)


d
str(d)

d[1]
d[2]
d$kids
d$ages

str(d)

library(data.table)
new.d <- fread("AWC.csv")
mean(AWC1$`2019-20 - APIP Approved`)
new.d
str(new.d)
c<- mean(new.d$`2019-20 - Funds Released`, na.rm = TRUE)
d<- sd(new.d$`2019-20 - Funds Released`)
c
d
summary(new.d)
#block2: operations

new.d[2:3,]

new.d[2:3,2]
head(new.d)
names(new.d)
new.d[new.d$`2019-20 - Funds Released`>100000,]
#how can we do this using subset?
S1 = subset(new.d, new.d$`2019-20 - APIP Approved` > 100000)

d4 <- data.frame(kids=c("Abhay", "X", "Manish", "Kavita"), age=c(10, 10, 12, 11),


stringsAsFactors = FALSE)
d4

#block2a

#cbind, rbind

rbind(d4,list("Asha", 9))
d4
d4 <- rbind(d4,list("Asha", 9))
d4
d4 <- data.frame(kids=c("Abhay", "X", "Manish", "Kavita"), age=c(10, 10, 12, 11),
stringsAsFactors = FALSE)
cbind(d4, d4$age+10)
d4
d4<-cbind(d4, d4$age+10)
d4

#block2b: lapply

lapply(new.d[,2:3], max)

#block3: merge

#merge(x,y) assumes that the two data frames have one or


#more columns with names in common

d1 <- data.frame(kids=c("Abhay", "Kavita", "Manish", "Asha"),


ages=c(11,10,9,13), stringsAsFactors = FALSE)
d1

d2 <- data.frame(kids=c("Abhay", "Kavita", "Shradha"),


class=c(5,6,4), stringsAsFactors = FALSE)
d2

merge(d1,d2)

d3 <- data.frame(pals=c("Abhay", "Kavita", "Shradha"),


class=c(5,6,4), stringsAsFactors = FALSE)
d3
merge(d1,d3)
merge(d1,d3,by.x = "kids", by.y = "pals")
merge(d3,d1,by.x = "pals", by.y = "kids")

#block4: applying functions

d5 <- lapply(d1, sort)


d5
str(d5)
#lapply calls sort() on each column, and returns a list

as.data.frame(d5)
#note: this is not very useful as the name-age correspondence is lost

#Exercise

#(1) Create a data frame as shown below

# item cond
#1 a
#1 b
#1 c
#1 d
#2 a
#2 b
#2 c
#2 d
#3 a
#3 b
#3 c
#3 d

#(2) Now merge the following data frame with the above

# cond RT
#a 300
#b 350
#c 280
#d 250

#(3) For each item substract the original RTs as follows


#
# item value
#1 100
#2 50
#3 200

You might also like