-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUsedCars.R
64 lines (62 loc) · 1.98 KB
/
UsedCars.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
pt.data <- data.frame(
id = c (1:6),
name = c("Jack","Lane","Terry","Claire","Jessica", "Ted"),
Age = c(35,21,31,26,24,12)
, stringsAsFactors = FALSE)
pt.data[1:2,]
pt.data[c(3,5),c(1,3)]
pt.data <- cbind(pt.data,Salary=c(10.11,23.11,33.11,41.23,9.23,50.11))
pt.data <- rbind(pt.data,list(7,"Tom",40,0.01))
//============================================================
usedcars.data <-read.csv("usedcars.csv")
str(usedcars.data)
summary(usedcars.data$price)
maxprice <-max(usedcars.data$price)
minyear <-min(usedcars.data$year)
autocars <- usedcars.data[usedcars.data$transmission == 'AUTO', ]
manualcars <- usedcars.data[usedcars.data$transmission == 'MANUAL', ]
averageautocardprice <- ave(autocars$price)
averagemanualcardprice <- ave(manualcars$price)
cheaper <-c()
yearssince <-c()
lessthanaverageprice <-c()
for (i in 1:length(usedcars.data$price)) {
cheaper[i] <- maxprice - usedcars.data$price[i]
yearssince[i] <- usedcars.data$year[i] - minyear
if(usedcars.data$transmission[i] == "AUTO")
{
if(usedcars.data$price[i] <= averageautocardprice)
{
lessthanaverageprice[i] <- TRUE
}
else
{
lessthanaverageprice[i] <- FALSE
}
}
else
{
if(usedcars.data$price[i] <= averagemanualcardprice)
{
lessthanaverageprice[i] <- TRUE
}
else
{
lessthanaverageprice[i] <- FALSE
}
}
}
usedcars.data[,"cheaper"] <- cheaper
usedcars.data[,"yearssince"] <- yearssince
usedcars.data[,"lessthanaverageprice"] <-lessthanaverageprice
# plots
library(gmodels)
hist(usedcars.data$price,main = "Histogram of Price", xlab = "Price", breaks = 20)
boxplot(usedcars.data$price, main="Boxplot of Price", ylab = "Price")
a <-"Year since"
b <- toString(minyear)
c <-"vs Amount cheaper"
title <- paste(a,b,c)
plot(x=usedcars.data$yearssince,y=usedcars.data$cheaper,main=title, xlab="Year(mile)", ylab = "Amount Cheaper($)")
CrossTable(usedcars.data$lessthanaverageprice, y=usedcars.data$transmission)
CrossTable(usedcars.data$color, y=usedcars.data$transmission)