Total number of cells in R numeric dataframe

7

2

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

Tomas

Posted 2013-07-25T07:23:16.113

Reputation: 2 333

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 – 2013-07-25T07:32:30.173

@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 – 2013-07-25T07:36:37.393

@Peter, I've added an example of input data. Please retract the downvote. – Tomas – 2013-07-25T07:43:22.360

Actually, "the question is unclear" is explicitly given as a reason for downvoting. I will try to clarify it. – Peter Taylor – 2013-07-25T10:31:11.510

@PeterTaylor this question seems pretty clear to me, perhaps because I am quite familiar with R. – Simon O'Hanlon – 2013-07-25T13:31:12.603

@SimonO101, you're looking at the seventh revision. – Peter Taylor – 2013-07-25T14:38:59.117

@PeterTaylor point taken. It is all the better for the revisions. – Simon O'Hanlon – 2013-07-25T14:43:45.963

Answers

10

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.

Spacedman

Posted 2013-07-25T07:23:16.113

Reputation: 216

@Tomas How do you get that md5sum? I get 093130007d6f8d5d43e6bbab25f68733 for a file containing only sum(x|T). – Christopher Bottoms – 2015-03-18T14:02:59.733

@ChristopherBottoms you have to remove the newline - it is without newline. – Tomas – 2015-03-19T08:06:52.840

@Tomas Thanks. I didn't think it had a newline, but xxd -p showed that it did. – Christopher Bottoms – 2015-03-19T18:40:06.267

Very good! But it still can be few characters shorter ... :) – Tomas – 2013-07-25T12:10:11.550

+1 joint honours I reckon unless Tomas has something sneaky up his sleeve! – Simon O'Hanlon – 2013-07-25T13:19:04.847

sum(x|T) is my solution!!! You can check the md5 sum now!!! :-) – Tomas – 2013-07-25T16:00:13.770

11

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

Simon O'Hanlon

Posted 2013-07-25T07:23:16.113

Reputation: 211

NA^0 is 1? Whooda thunk it? – Spacedman – 2013-07-25T13:16:58.073

@Spacedman I know! It makes sense though right. NA is missing data, but anything ^ to 0 is 1. – Simon O'Hanlon – 2013-07-25T13:18:34.767

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 – 2013-07-25T13:22:26.147

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 – 2013-07-25T13:24:43.027

@Spacedman what is even more worrying is that is.numeric(NaN) == TRUE :-) As well as in javascript, "Not a Number" IS number :D – Tomas – 2013-07-25T16:03:59.067

you both guys now have the same length solution as me. Let's see if someone can make something even shorter :-) – Tomas – 2013-07-25T16:06:22.317

@Tomas is yours the same as one of ours or do you have a third one that is the same length? – Simon O'Hanlon – 2013-07-25T16:07:00.570

@SimonO101 see the comment on Spacedmans answer – Tomas – 2013-07-25T16:08:32.887

sum(1^x) worked as well – plannapus – 2013-07-26T07:18:09.453