Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- using System.IO;
- using System.Collections.Generic;
- namespace cs_console
- {
- class CsvStats
- {
- readonly IEnumerable<IEnumerable<int>> lines;
- public CsvStats(string filename)
- {
- lines = File
- .ReadAllLines(filename)
- .Skip(1)
- .Select(line => line
- .Trim()
- .Split(new[] { ',' })
- .Select(e=>int.Parse(e))
- )
- ;
- }
- public double SumOfColumn(int col)
- {
- return Column(col).Sum();
- }
- public double MeanOfColumn(int col)
- {
- return SumOfColumn(col) / Column(col).Count();
- }
- IEnumerable<int> Column(int col)
- {
- return lines
- .Select(line => line.Skip(col).First())
- ;
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- try
- {
- GenerateData();
- var stats = new CsvStats("test.csv");
- Console.WriteLine($"1[MEAN]: {stats.MeanOfColumn(1)}");
- Console.WriteLine($"2[SUM]: {stats.SumOfColumn(2)}");
- }
- catch (Exception e)
- {
- Console.WriteLine(e);
- }
- }
- static void GenerateData()
- {
- var text = @"col 0,col 1,col 2
- 542,414,26
- 335,950,113
- 378,954,231";
- File.WriteAllText("test.csv", text);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement