Combining head and tail methods in R

I use the head (d) and tail (d) methods in the R package, which are often used - often one by one. Therefore, I wrote a simple wrapper for two functions:

ht <- function(d, m=5, n=m){ # print the head and tail together cat(" head --> ", head(d,m), "\n", "--------", "\n", "tail --> ", tail(d,n), "\n") } 

And I got unexpected results ... can someone please help me understand why? (so that I can fix it ... or at least understand your decision!).

Some prerequisites ...

The number is in order:

 x <- 1:100 ht(x) 

How difficult:

 ni <- as.complex(1:100) ht(ni) 

and symbol:

 ll <- letters[1:26] ht(ll) 

The matrix loses its structure, returning [1,1] in [5,5] + [16,1] to [20,5], but as two vectors - compare:

 m <- matrix(1:10, 20) ht(m) 

in

 head(m, 5) tail(m,5) 

I would like to preserve the matrix structure, how do utils methods do this - is this possible?

Finally (well, maybe more errors, this is exactly where I get) data.frames is a mess:

 df <- data.frame(num=x[1:26], char=ll) ht(df) 

this leads to the following error:

 head --> Error in cat(list(...), file, sep, fill, labels, append) : argument 2 (type 'list') cannot be handled by 'cat' 

Steps so far:

As the utils method keeps the matrix in order when executed in bits, I tried to fix the problem with the following change:

 function(d, m=5, n=m){ # print the head and tail together rb <- rbind(head(d, m), tail(d,n)) if (class(d) == 'matrix'){ len <- nrow(rb) cat(" head --> ", rb[(1:m),], "\n", "--------", "\n", "tail --> ", rb[((len-n):len),], "\n") } else cat(" head --> ", rb[1,], "\n", "--------", "\n", "tail --> ", rb[2,], "\n") } 

That seems to have done nothing with the matrix ... and still breaks with the same error when i

 ht(df) 

I guess from the errors that there is some problem with cat () here, but I cannot figure out what it is or how to fix it.

Can anybody help?

+4
source share
2 answers

Why not change your function to list instead?

 ht <- function(d, m=5, n=m){ # print the head and tail together list(HEAD = head(d,m), TAIL = tail(d,n)) } 

Here is the output for your matrix and data.frame :

 ht(matrix(1:10, 20)) # $HEAD # [,1] # [1,] 1 # [2,] 2 # [3,] 3 # [4,] 4 # [5,] 5 # # $TAIL # [,1] # [16,] 6 # [17,] 7 # [18,] 8 # [19,] 9 # [20,] 10 ht(data.frame(num=x[1:26], char=ll)) # $HEAD # num char # 1 1 a # 2 2 b # 3 3 c # 4 4 d # 5 5 e # # $TAIL # num char # 22 22 v # 23 23 w # 24 24 x # 25 25 y # 26 26 z 
+7
source

It has been suggested that I will return my comment in return.

In your R console, when you type head(m, 5) , what you see on the screen is really the result of print(head(m, 5)) . Therefore, if you want your output to look, consider using the print function rather than cat when displaying the objects of head and tail your objects:

 ht <- function(d, m=5, n=m) { # print the head and tail together cat("head -->\n") print(head(d,m)) cat("--------\n") cat("tail -->\n") print(tail(d,n)) } m <- matrix(1:10, 20) ht(m) # head --> # [,1] # [1,] 1 # [2,] 2 # [3,] 3 # [4,] 4 # [5,] 5 # -------- # tail --> # [,1] # [16,] 6 # [17,] 7 # [18,] 8 # [19,] 9 # [20,] 10 

I find @mrdwab's answer to be a very elegant solution. It does not explicitly use print , but returns a list. However, when its function is called from the R console, and the output is not assigned by anything, it is displayed on the console (therefore, print used implicitly). Hope this helps you understand what is happening.

+4
source

All Articles