Replacing NaN Values

Hey guys so I have these text files that I populated with data from reading and storing values from a CSV. Now I have to "Clean" the data by replacing missing data or zeros/nan's with the value that is located previously, afterward or and average. I am a bit confused with how to go about doing this. Are there any ideas on how this could be done.
Depends on the format of the file, you should show us your attempt at what you've tried.

You'd have some sort of loop, and a variable to keep track of the last good value (if replacing with the "value that is located previously").

The "missing" check depends on the format of the file.
The 0 check is easy enough, just if(data[i] == 0).
The NaN check can be done with isnan: http://www.cplusplus.com/reference/cmath/isnan/

Edit: Actually, I just looked this up, realizing I've never actually tried to read a NaN file directly from a file. Parsing for NaN values is harder than a simple value parse as well. It again depends on the format of your file.
This link might help: https://stackoverflow.com/questions/11420263/is-it-possible-to-read-infinity-or-nan-values-using-input-streams
Last edited on
Try this piece of code bro

1
2
3
4
5
6
7
8
9
10
11
void nantoZero(string &line)
{
	string zero("0");
	size_t pos = line.find_first_of("NaN");

	while (pos != string::npos)
	{
		line.replace(pos, 3, zero);	
		pos = line.find_first_of("NaN", pos + 1);
	}
}
Last edited on
Topic archived. No new replies allowed.