Reputation: 908
Really simple question, to which I haven't been able to Google/find an answer.
I have a simple file - "test.txt"
Gender arch1
M 99
F 97
M NA
I load this into a resultset using read.table - works fine.
attach(resultset)
Now, I want _something_ that will give me
mean(arch1, [_SOMETHING_]) = 65.3 (and *_NOT_* 98)
i.e. not na.rm = T or similar. Basically, I want to treat NA as 0 - and I don't want to do something complicated like here with matrices and dataframes.
Just NA = 0 from the file. Surely, I can't be the first person to have wanted this? And I don't want discussions about how NA and 0 are not the same thing. I think it's perfectly valid to want to calculate an average even over missing students!
Upvotes: 0
Views: 1088
Reputation: 908
Below is my suggestion on how to implement the solution to which I have now given as the correct answer:
avg_without_NA = function(vec)
{
my_ans = sum(vec, na.rm = T)/length(vec)
return(my_ans)
}
So, now all I have to do is make a simple function call and my code is encapsulated in! Simpler all round, and easier to read!
Upvotes: -1
Reputation: 81693
You can replace the NA
s with zeros before using mean
:
mean(replace(arch1, is.na(arch1), 0))
# [1] 65.33333
Upvotes: 5
Reputation: 46
you could replace the NA by zero with resultset$arch1[is.na(resultset$arch1)]=0
and then compute the mean of the arch1
column
Upvotes: 0