# Total number of cells in R numeric dataframe

## Problem statement

The task is to write the shortest R statement to count the cells (rows * cols) in an instance of the R type numeric data.frame. For example, the data.frame which would be displayed as

  a    b  c
1 1 2000 NA
2 2 2000 NA
3 3 2000 1
4 4 2000 Inf


has 12 cells.

## Input

The data.frame is supplied in variable x. You may assume that it contains only numbers, boolean, NA, Inf (no strings and factors).

## Test cases

Example code to construct your own data.frame:

x <- merge(data.frame(a = 1:100),
merge(data.frame(b = 2000:2012),
data.frame(c = c(NA, 10, Inf, 30))))


The expected output is 15600.

## Appendix

Curious if someone can beat my solution :-) I don't want to take the fun from you so I will not reveal it now, I will only show you md5sum of my solution so that you later trust me in case you invent the same :-)

d4f70d015c52fdd942e46bd7ef2824b5

• One example of input does not suffice to be a spec. What are the delimiters? Are the row and column headers guaranteed to be present? Can a cell be empty, and if so how is that represented? Also, you talk about people beating your solution, but you haven't specified a criterion for comparison. – Peter Taylor Jul 25 '13 at 7:32
• @Peter, this is R challenge. You get x as a variable of type data.frame. It is not text input. I specified that in the question already. Ad the criterion - please read the question: "construct as short command as possible". Please if you don't understand, this is not reason for downvote. – Tomas Jul 25 '13 at 7:36
• @Peter, I've added an example of input data. Please retract the downvote. – Tomas Jul 25 '13 at 7:43
• Actually, "the question is unclear" is explicitly given as a reason for downvoting. I will try to clarify it. – Peter Taylor Jul 25 '13 at 10:31
• @PeterTaylor this question seems pretty clear to me, perhaps because I am quite familiar with R. – Simon O'Hanlon Jul 25 '13 at 13:31

Naive:

> nrow(x)*ncol(x)
[1] 15600


First idea:

> prod(dim(x))
[1] 15600


Best I can do so far:

> length(!x)
[1] 15600


SimonO10 on the R chat had an idea (sum(!(F&x))), which I modded to get:

> sum(T|x)
[1] 15600


Note this doesn't work on factors, but you excepted those.

• Very good! But it still can be few characters shorter ... :) – Tomas Jul 25 '13 at 12:10
• +1 joint honours I reckon unless Tomas has something sneaky up his sleeve! – Simon O'Hanlon Jul 25 '13 at 13:19
• sum(x|T) is my solution!!! You can check the md5 sum now!!! :-) – Tomas Jul 25 '13 at 16:00
• @Tomas How do you get that md5sum? I get 093130007d6f8d5d43e6bbab25f68733 for a file containing only sum(x|T). – Christopher Bottoms Mar 18 '15 at 14:02
• @ChristopherBottoms you have to remove the newline - it is without newline. – Tomas Mar 19 '15 at 8:06

My best two attempts so far only got me down to 11 characters. I'd love to see fewer than @Spacedman's 10!

>sum( [<-(x,,,1) )
[1] 15600

>sum(!(F&x))
[1] 15600


# Got it!

sum(x^0)
[1] 15600

• NA^0 is 1? Whooda thunk it? – Spacedman Jul 25 '13 at 13:16
• @Spacedman I know! It makes sense though right. NA is missing data, but anything ^ to 0 is 1. – Simon O'Hanlon Jul 25 '13 at 13:18
• yes, but NA isn't anything :) It's absence of thing. Although I guess the argument is that even if you have a missing measurement for something you know that whatever it was to the zeroth power is going to be 1. NaN^0 == 1 is a bit more worrying (philosophically) though. – Spacedman Jul 25 '13 at 13:22
• Agreed. I thought NA was a placeholder for missing data. Sicne this is numeric data it has to be a missing number therefore it has to resolve to 1. FWIW if you try x^0 on character column with NAs you correctly get an error. – Simon O'Hanlon Jul 25 '13 at 13:24
• @Spacedman what is even more worrying is that is.numeric(NaN) == TRUE :-) As well as in javascript, "Not a Number" IS number :D – Tomas Jul 25 '13 at 16:03