Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Age i want to calculate the median of that column along with other values which are higher or lower than the average value

What I have tried:

.
Posted
Updated 25-Mar-18 6:09am
v4

Here is an interesting library for reading and writing CSV files: GitHub - JoshClose/CsvHelper: Library to help reading and writing CSV files[^]

You can also use LINQ, here is an example to calculate the average age:
// Read CSV using LINQ
static void ReadFileLinq(string fileName)
{
    IEnumerable<string> strCSV = File.ReadLines(fileName, Encoding.ASCII).Skip(1);

    var results = from str in strCSV
                  let tmp = str.Split(',')
                  .Select(x => x.Replace("?", string.Empty))
                  select new { Age = DateTime.Parse(tmp.ElementAt(0))};

    // caching for performance
    var query = results.OrderBy(x => x.Age));

    foreach (var item in query)
    {
        Console.WriteLine(item.Age);
    }

    Console.WriteLine(string.Empty);
    Console.WriteLine("Average age = " + query.Average(x => x.Age));
    Console.WriteLine(string.Empty);
}

As LINQ does not have a Median function, you will have to write this function yourself.
Here is an example on CodeProject: Using LINQ to Calculate Basic Statistics[^]
 
Share this answer
 
v2
Comments
Maciej Los 25-Mar-18 10:28am    
5ed!
Start by working out how you are going to read the CSV data: there are a whole load of ways including writing your own CSV processor, but A Fast CSV Reader[^] is a good way to start.
When you have the data, processing the column to get the average, median, or mean is trivial!
 
Share this answer
 
Comments
Maciej Los 25-Mar-18 10:28am    
5ed!
It's easy to calculate median[^], because it's a middle/central value* of ordered list.
* - for even number of values - it's an average of two values from middle of ordered list
Follow the above link for further details.

C#
//case #1 - even number of values
//int[] ages = {100,22,13,43,25,61,17,80}; 
//result: AVG(25,43) = 34

//case #2 - odd number of values
int[] ages = {100,22,13,43,25,61,17,80,29}; 
//result: 29

//does the number of values is even?
bool isEvenNumberOfValues = ages.Length % 2 == 0;
int toSkip = isEvenNumberOfValues ? (ages.Length/2)-1 : ages.Length/2;
int toTake = isEvenNumberOfValues ? 2 : 1;
//get middle value from ordered list 
var result = ages.OrderBy(x=>x).Skip(toSkip).Take(toTake).Average();
Console.WriteLine("Median for: [{0}] is {1}", string.Join(",", ages.OrderBy(x=>x)), result);


All you need to do is to create Median function (based on above code). A Median function have to:
1. take an array of numbers as an input argument
2. return double

Another implementation of Median function, you'll find here: How to: Add Custom Methods for LINQ Queries (C#) | Microsoft Docs[^]
 
Share this answer
 
v2
Comments
RickZeeland 25-Mar-18 11:29am    
pięćd :)
Maciej Los 25-Mar-18 11:52am    
Muchas gracias!
;)
Quote:
I want to read Csv files that is there in sample(name) folder

2 steps:
- get the list of csv files in directory
- read them 1 by 1

You may need to learn programming.
 
Share this answer
 
v3
Comments
Maciej Los 25-Mar-18 10:32am    
Although your answer is incomplete, i voted 5 to counteract down-vote.
Patrice T 25-Mar-18 10:39am    
Thank you.
The question is not very complete either.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900