-
Notifications
You must be signed in to change notification settings - Fork 0
/
.Rhistory
141 lines (141 loc) · 6.59 KB
/
.Rhistory
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
setwd("/media/david/Linux/Wrangling_CIRM_Data/DogTools")
getwd()
list.files()
# this causes error EOF within quoted string
# Bork <- read.delim("Bork.sh")
# same error EOF within quoted string
# Bork <- read.delim("Bork.sh")
Bork <- readLines("Bork.sh")
# this text file is 240 characters long
length(Bork)
# Here's the whole file
# It's a shell script that prints ASCII art of dogs
# AND it prints out a dog "running" across the screen
Bork
# some quick manual analysis showed that the dog's position was moving across the screen
# in a strange pattern ... why not just the same repetitive distance?
# these are the character positions of the dog's tail
starting_positions <- c(1, 9, 25, 41, 65, 97)
# these are the names for the cat commands (sleep 1 runs between each)
position_names <- c("d1", "dor", "dork", "dorkl", "dorkly")
# I could do some more analysis with ^those numbers, but it won't be as cool as
# using R to get the info!
# this was just a test to find out if I was iterating through the file properly
# I used modulo to print out every third line
i <- 1
while(i <= length(Bork)){
if(i %% 3 ==0){
print(Bork[i])
}
i <- i + 1
}
# how can I get the position of the dog with R?
# the tail has a ":", BUT that character is in the earlier dog pictures.
# I could use logic to only start looking for ":" after seeing a "d" character
# (cause the calls to the dog running all start with d ["d1", "dor", "dork", "dorkl", "dorkly"])
# But, there's a more elegant way! The "U" character is used for the dog's tongue.
# there is no other place that has a "U" (line 1 has "u" in "#/usr/bin/bash")
# luckily "grepl" can distinguish between upper and lower case characgers
# this prints "FOUND A "U"!" When it finds a "U"
i <- 1
number_of_U <- 0
while(i <= length(Bork)){
#print(Bork[i])
if(grepl("U", Bork[i])){
print("FOUND A 'U'!")
number_of_U <- number_of_U + 1
}
i <- i + 1
}
print(number_of_U)
library(stringr)
# I'll need x position to calculate velocity and grepl only returns TRUE/FALSE
# gregexpr returns position though! BUT, it can't review multiple lines with ease
# I could use gregexpr, but it'd return "-1" for cases without "U"
# it's simpler to just use grepl to find the lines to run gregexpr on
i <- 1
lines_with_slobber <- list()
slobber_x_position_list <- list()
number_found_dogs <- 0
# position values
current_second <- 0
current_slobber_x_position <- 0
last_slobber_x_position <- 0
number_of_position_data_points <- 0
# velocity values
current_slobber_velocity <- 0
last_slobber_velocity <- 0
# acceleration values
current_slobber_acceleration <- 0
last_slobber_acceleration <- 0
# creating lists for position, velocity, and acceleration
position_list <- list()
velocity_list <- list()
acceleration_list <- list()
time_list <- list()
while(i <= length(Bork)){
#print(Bork[i])
number_of_tabs <- 0
if(grepl("U", Bork[i])){
print("========================================================================")
number_found_dogs <- number_found_dogs + 1
print(paste("Dog Number: ", number_found_dogs))
print(paste("FOUND A 'U' at line ", i))
slobber_x_position_list[number_found_dogs] <- gregexpr(pattern = 'U', Bork[i])[[1]]
print(paste("slobber position is: ", slobber_x_position_list[number_found_dogs]))
lines_with_slobber[number_found_dogs] <- Bork[i]
print("line is: ")
print(lines_with_slobber[number_found_dogs])
number_of_tabs <- str_count(lines_with_slobber[number_found_dogs], "\t")
print(paste("The number of tabs is ", number_of_tabs))
# tabs are 8 spaces, BUT the characters of "\t" take up two spaces, so it's (8-2)*number_of_tabs + slobber_x_position
tab_corrected_slobber_position <- (8-2) * number_of_tabs + as.numeric(slobber_x_position_list[number_found_dogs])
# this keep track of the number of position data points (1 gets position, 2 gets velocity, 3 gets acceleration)
number_of_position_data_points <- number_of_position_data_points + 1
print(paste("tab-corrected slobber position is ", tab_corrected_slobber_position))
# renaming the variable to have a better name to work with
current_slobber_x_position <- tab_corrected_slobber_position
current_second <- current_second + 1
print(paste("number of data points: ", number_of_position_data_points))
if(number_of_position_data_points == 0) {
# this shouldn't run w/ the inputs I can think of
print("lol ... something went wrong")
} else if(number_of_position_data_points == 1) {
# this is the position case
print(paste("Position ", current_slobber_x_position))
#last_slobber_x_position <- current_slobber_x_position
} else if(number_of_position_data_points == 2) {
# this is the position, velocity case
current_slobber_velocity <- current_slobber_x_position - last_slobber_x_position
print(paste("Position ", current_slobber_x_position, " Velocity ", current_slobber_velocity))
#last_slobber_x_position <- current_slobber_x_position
#last_slobber_velocity <- current_slobber_velocity
} else if(number_of_position_data_points > 2) {
# this is the position, velocity, acceleration case
current_slobber_velocity <- current_slobber_x_position - last_slobber_x_position
current_slobber_acceleration <- current_slobber_velocity - last_slobber_velocity
print(paste("Position ", current_slobber_x_position, " Velocity ", current_slobber_velocity, " Acceleration ", current_slobber_acceleration))
#last_slobber_x_position <- current_slobber_x_position
#last_slobber_velocity <- current_slobber_velocity
#last_slobber_acceleration <- current_slobber_acceleration
} else {
print("David didn't take all the cases into account :(")
}
}
# saving x, velocity and acceleration into lists for plotting
position_list[current_second] <- current_slobber_x_position
velocity_list[current_second] <- current_slobber_velocity
acceleration_list[current_second] <- current_slobber_acceleration
time_list[current_second] <- current_second
# delta x will be used for velocity next round
last_slobber_x_position <- current_slobber_x_position
# delta veloctiy will be used for acceleration last round
last_slobber_velocity <- current_slobber_velocity
i <- i + 1
}
plot(time_list, as.numeric(position_list), xlab="Time (seconds)", ylab="Slobber Position (characters)", main="ASCII Dog Slobber Position Over Time", col="blue", type="b", pch=19, lty=2)
lm(as.numeric(position_list) ~ as.numeric(time_list))
abline(-7.733, 18.971, col="red")
legend(1, 100, legend=c("Line of Fit", "Slobber Position"), col=c("red", "blue"), lty=1:2, cex=0.8)
plot(time_list, velocity_list, xlab="Time (seconds)", ylab="Velocity (characters/second)", main="ASCII Dog Velocity Over Time", col="brown", type="b", pch=19, lty=1)
plot(time_list, acceleration_list, xlab="Time (seconds)", ylab="Acceleration (characters/second^2)", main="ASCII Dog Acceleration Over Time", col="purple", type="b", pch=19, lty=1)