0

function CSVsum(filename){
  var res = [];
  //Open the file and split into rows based on linebreaks
  var file = fs.readFileSync(filename,{encoding: 'utf-8', flag:'r'}).split('\n');
  console.log(file)
  //Split each item in table into values.
  for (i = 0; i < file.length; i++) {
    file[i] = file[i].split(',');
  }
  //Using linebreaks to split rows creates an empty final value, so use .pop to remove
  file.pop()

  //Function to transpose rows into columns
  const transpose = arr => {
    for (let i = 0; i < arr.length; i++) {
      for (let j = 0; j < i; j++) {
        const tmp = arr[i][j];
        arr[i][j] = arr[j][i];
        arr[j][i] = tmp;
      }
    }
  }
  //Run transpose function on file
  transpose(file)
  console.log(file)
  for(let i=0; i<file.length; i++){
    let column = file[i];
    let columnSum= 0.0;
    for(let i=0; i<column.length; i++){
      let value = parseFloat(column[i]);
      if(isNaN(value) === false){
        columnSum += value;
      }
    }
    res.push(parseFloat(columnSum));
  }
  return res
}

Basically I've written the above function. Its intended function should be to read a CSV, then sum each column. For the exercise, I am not allowed to import anything additional.

The function works for the most part, except the transpose function for some reason adds an additional array object full of 'undefined'. As a result, where the output should be x,y,z it is instead x, y, z, 0. I know I could use .pop to remove the final item, but I've already done that once when I would've rather not used it at all. What am I doing wrong? I managed to achieve the desired result in python with zip(*file) so I'm trying for a similar effect here.

P.S. I know this isn't the best, so if anyone notices anything else in need of improvement please don't hesitate to let me know!

  • The best practice format of comments is a space after the // Like: `// This is a comment` – Branchverse Dec 10 '21 at 21:22
  • 1
    Time to reduce your code to find the problem: if the transpose does something odd, remove everything else and run your transpose with a small, hardcoded, data input so you can easily track what happens at each step. – Mike 'Pomax' Kamermans Dec 10 '21 at 21:24
  • could you provide an example File? to use for the function? – Branchverse Dec 10 '21 at 21:24
  • [Are your columns less than the rows?](https://jsbin.com/ciwahehemo/1/edit?js,console) – VLAZ Dec 10 '21 at 21:25
  • Your transpose function looks fine. I suspect you have an extra row when reading the file. – Barmar Dec 10 '21 at 21:25
  • @VLAZ Yes some of the files do have more rows than columns. I think this is the issue then, but not sure how to fix it. Theoretically, the function should be able to sum the values of any CSV columns regardless of how many rows/columns there are. I did it in python by doing zip(*file) so trying to replicate the same effect in js. – user17555430 Dec 10 '21 at 21:33
  • @Barmar there are 4 rows and 3 cols in the doc I tried. I'm not sure how to transpose it in a way that works with different sizes of CSV. I wrote the function in python originally and transposed by doing zip(*file) – user17555430 Dec 10 '21 at 21:41
  • Your transpose function is identical to one [here](https://stackoverflow.com/a/46805290/1491895) so I think it's correct. – Barmar Dec 10 '21 at 21:49
  • If the doc has 4 rows and 3 columns, the transposed doc should have 3 rows and 4 columns. x, y, z is only 3 columns. – Barmar Dec 10 '21 at 21:50
  • @Barmar unfortunately not. The test CSV i'm using has 3 cols, headers var1, var2 and var 3, with 3 values under each header. The original doc when read comes through as 4 lists. List 1 is [var 1, var2, var3] and then the remaining lists are each rowsvs values. When I transpose, it works in outputting lists like [var 1, x, y, z] but then has a 4th list of [undefined, undefined, undefined, undefined] – user17555430 Dec 12 '21 at 15:29
  • Your `transpose()` function only works when the number of rows is the same as columns, but you have 4 rows (when including the header). There are lots of other algorithms in the question I linked to above, they may work better for non-square matrixes. – Barmar Dec 12 '21 at 23:28

0 Answers0