7
\$\begingroup\$

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

\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – Peter Taylor Jul 25 '13 at 7:32
  • \$\begingroup\$ @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. \$\endgroup\$ – Tomas Jul 25 '13 at 7:36
  • \$\begingroup\$ @Peter, I've added an example of input data. Please retract the downvote. \$\endgroup\$ – Tomas Jul 25 '13 at 7:43
  • \$\begingroup\$ Actually, "the question is unclear" is explicitly given as a reason for downvoting. I will try to clarify it. \$\endgroup\$ – Peter Taylor Jul 25 '13 at 10:31
  • \$\begingroup\$ @PeterTaylor this question seems pretty clear to me, perhaps because I am quite familiar with R. \$\endgroup\$ – Simon O'Hanlon Jul 25 '13 at 13:31
10
\$\begingroup\$

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.

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

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
\$\endgroup\$
  • \$\begingroup\$ NA^0 is 1? Whooda thunk it? \$\endgroup\$ – Spacedman Jul 25 '13 at 13:16
  • \$\begingroup\$ @Spacedman I know! It makes sense though right. NA is missing data, but anything ^ to 0 is 1. \$\endgroup\$ – Simon O'Hanlon Jul 25 '13 at 13:18
  • \$\begingroup\$ 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. \$\endgroup\$ – Spacedman Jul 25 '13 at 13:22
  • \$\begingroup\$ 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. \$\endgroup\$ – Simon O'Hanlon Jul 25 '13 at 13:24
  • \$\begingroup\$ @Spacedman what is even more worrying is that is.numeric(NaN) == TRUE :-) As well as in javascript, "Not a Number" IS number :D \$\endgroup\$ – Tomas Jul 25 '13 at 16:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.