I think the problem comes from splitting the data using \r\n
which is OS dependant: if you're on Linux, it should be contents.split('\n')
. Either way, you probably would be better off using IO#each
to iterate over every line in the file and let Ruby deal with line ending characters.
data = File.open("test.txt", "r+")
count = 0
sum = 0
variance = 0
data.each do |line|
value = line.split(',')[1]
sum = sum + value.to_f
count += 1
end
avg = sum / count
puts "The average of your large data set is: #{ avg.round(3)} (Answer is rounded to nearest thousandth place)"
# We need to get back to the top of the file
data.rewind
data.each do |line|
value = line.split(',')[1]
variance = variance + (value.to_f - avg)**2
end
variance = variance / count
variance = Math.sqrt(variance)
puts "The standard deviation of your large data set is: #{ variance.round(3)} (Answer is rounded to nearest thousandth place)"