필터 지우기
필터 지우기

Add a new column to CSV file with equations

조회 수: 2 (최근 30일)
Stef1998
Stef1998 2021년 11월 18일
댓글: dpb 2021년 11월 23일
I have a large data set
17568 rows x 12 columns in an xslx and csv file (1st row header, the rest numbers, dates, year etc)
I need to create a new column that is the cummulative sum of a column in the data set. If possible that also makes Na values 0.
example:
Year A B
2001 3 2
2001 4 1
I need to make:
Year A B CummA CummB
2001 3 2 3 2
2001 4 1 7 3

채택된 답변

dpb
dpb 2021년 11월 18일
[tTable array2table(cumsum(tTable{:,{'A','B'}}),'VariableNames',{'cumA','cumB'})];
where tTable is your table variable.
One can get fancy and use the table properties property 'VariableNames' to build the name fields dynamically as well.
As for the NaN, there's an optional flag with cumsum to ignore NaN in the calculations if that's the result you wish; it effectively inserts a zero in place of the value by ignoring it in computing the sums--it does NOT replace the NaN in the data itself.
You've got to decide which way you want to go...it's trivial exercise to just replace NaN values with zeros first in the original data, but then, of course, they're gone and if there are zero values that are inherent in the data to begin with, then you've lost the distinction.
  댓글 수: 3
Peter Perkins
Peter Perkins 2021년 11월 23일
It's also possible to avoid horzcat and array2table by assigning directly into tTable, as shown here:
dpb
dpb 2021년 11월 23일
Good point, Peter...I just took the expedient route. The concatenation is probably measurably slower.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by