-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathR_Practice_Code.R
More file actions
123 lines (87 loc) · 2.4 KB
/
Copy pathR_Practice_Code.R
File metadata and controls
123 lines (87 loc) · 2.4 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# Practice1- Find out the difference between 689 and 53 using R as a calculator
689-53
# P2- Assign values to variables and do computation
x<-9
y<- 16
x+y
x-y
sqrt(x)
sqrt(y)
sqrt(x)+sqrt(y)
# P3- Create a vector using 'concatenate' or 'combine' function
z <- c(1,2,3,5,9,10)
z
z*2+150
# P4- Vector "Recyling"
c(1,2,3,4) + c(0,10)
# P5- Get working directory and set working directory
getwd()
setwd("C:/Users/rpand/Desktop/Documents/Classes/Classes/basic_stats")
# P6- list all objects in the workspace
ls()
# P7- list all files in the working directory
list.files()
# P8- Getting help on any function
?list.files()
#P9- Sequencing
1:15
15:0
pi:20
seq(0,20,0.5)
seq(1,20, length=40)
rep(c(1,2,3), times=25)
rep(c(1,2,3), each=25)
#P10- Reading data
cars <- read.csv("Worksheet in statistics_intro_day1n2.csv")
# P11- Subsetting dataframe
newcars1 <- cars[1:10,1:3]
newcars2 <- cars[1:10, c(1,8,9)]
newcars3 <- cars[1:10, c("Origin", "Year")]
# P12 - Find rows with max, min etc.
newcars4<- cars[which.max(cars$MPG),]
newcars5<- cars[cars$MPG == max(cars$MPG),]
newcars6<- cars[which.min(cars$MPG),]
newcars7<- cars[cars$MPG == min(cars$MPG),]
# P13- Creating New Variables in the dataset
cars$MPGpCYL <- cars$MPG/cars$Cylinders
cars$Mileage <- ifelse(cars$MPG >= 30, "Good", "Not so Good")
# P14- Summarizing data
head(cars)
tail(cars)
str(cars)
dim(cars)
str(cars)
names(cars)
ncol(cars)
nrow(cars)
class(cars)
# P15- Descriptive Stats
attach(cars)
mean(MPG)
median(MPG)
mode(MPG)
var(MPG)
sd(MPG)
range(MPG)
min(MPG)
max(MPG)
quantile(MPG, seq(0,1,0.05))
#Correlations
cor(newcars, use="complete.obs", method="pearson")
cor(newcars$MPG, newcars$Weight)
# Count of cars by Origin
library(plyr) # install.packages("plyr")
count(cars, 'Origin')
# Write the output to Excel (csv) file.
write.csv(count(cars, 'Origin'), "Countsheet.csv")
# Means, max, min by Origin
aggregate(MPG, list(Origin), mean)
aggregate(MPG, list(Origin), max)
aggregate(MPG, list(Origin), min)
# P16- plots
library(ggplot2) # may need to install this package using install.packages()
library(gridExtra)
p <- ggplot(cars, aes(Origin, MPG))
p + geom_boxplot(aes(fill = Origin))
p1 <- ggplot(cars, aes(x=Horsepower, y= Weight))
p1+geom_point(aes(color=factor(Origin))) + scale_color_manual(values = c("Green", "Purple", "Orange"))