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
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 typedata.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