Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8261857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T03:33:35+00:00 2026-06-08T03:33:35+00:00

I use the head(d) and tail(d) methods in R package utils a lot –

  • 0

I use the head(d) and tail(d) methods in R package utils a lot – frequently one after the other. So I wrote a simple wrapper for the 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 some unexpected results. Can someone please help me understand why? (so i can fix it or at least understand your solution!).

Some background…

Numeric values work fine:

x <- 1:100
ht(x)

So does complex:

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

and characters:

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

Matrix loses it’s structure, returning [1,1] to [5,5] + [16,1] to [20,5] but as two vectors — compare:

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

to:

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

I would like to keep the matrix structure, as the utils methods does – is this possible?

Finally (well, there may be more bugs, this is just where I’m up to) data.frames are a mess:

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

This yields 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 tidy when done in bits, I tried to fix the problem with the following edit:

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")
}

Which does not seem to have done anything to the matrix and still breaks with the same error when I use:

ht(df)

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

Can anyone please help?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T03:33:37+00:00Added an answer on June 8, 2026 at 3:33 am

    Why not modify your function to output a 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’s 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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I can use urllib2 to make HEAD requests like so: import urllib2 request =
I am just now starting to use version control and my head is spinning!
So right now I'm bashing my head - at the moment we use an
It would be nice to use ember.js , data from git HEAD , since
I'm learning sml, and wrote the following simple function: (* Return a list with
isTogether' :: String -> Bool isTogether' (x:xs) = isTogether (head xs) (head (tail xs))
I'm getting this error and I can't make head or tail of it. The
object *head = NULL, *tail = NULL; // EDIT struct object { vector <int>
It is demonstrated below: while (head == tail) { ....if (head == 100) ....{
I was reading this post While or Tail Recursion in F#, what to use

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.