Using R compute the average of all even numbers from 2100 Sh
Using R, compute the average of all even numbers from 2-100. Show commands and work. ( I can figure the answer out but not with correct commands in R)
Solution
sum = 0 # initialize sum to 0
count = 0
for (num in 2:100) # for loop works from 2 to 100
{
if((num%%2) == 0) # modulus operator %% is used to check if num is even
{
sum = sum + num # if num is even add it to sum
count = count + 1
}
}
sum = sum/count # compute average
print(sum)
Output
