结合R中的头尾方法

ric*_*rdo 4 r tail head cat dataframe

我在R package utils中经常使用head(d)和tail(d)方法 - 经常一个接一个地使用.所以我为这两个函数写了一个简单的包装器:

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")
}
Run Code Online (Sandbox Code Playgroud)

我得到了一些意想不到的结果......有人可以帮我理解为什么吗?(所以我可以解决它......或者至少了解你的解决方案!).

一些背景......

数字很​​好:

x <- 1:100
ht(x)
Run Code Online (Sandbox Code Playgroud)

很复杂:

ni <- as.complex(1:100)
ht(ni)
Run Code Online (Sandbox Code Playgroud)

和性格:

ll <- letters[1:26]
ht(ll)
Run Code Online (Sandbox Code Playgroud)

矩阵失去了它的结构,将[1,1]返回到[5,5] + [16,1]到[20,5]但是作为两个向量 - 比较:

m <- matrix(1:10, 20)
ht(m)
Run Code Online (Sandbox Code Playgroud)

至:

head(m, 5)
tail(m,5)
Run Code Online (Sandbox Code Playgroud)

我想保留矩阵结构,就像utils方法那样 - 这可能吗?

最后(好吧,可能有更多错误,这就是我要去的地方)data.frames是一团糟:

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

这会产生以下错误:

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

目前为止的步骤:

由于utils方法在完成比特时保持矩阵整洁,我尝试通过以下编辑来解决问题:

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")
}
Run Code Online (Sandbox Code Playgroud)

这似乎没有对矩阵做任何事情......并且当我发现时仍然会出现同样的错误

ht(df)
Run Code Online (Sandbox Code Playgroud)

我猜错了cat()这里有一些问题,但我无法弄清楚它是什么或如何修复它.

有人可以帮忙吗?

A5C*_*2T1 7

为什么不修改你的函数输出列表呢?

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

这是你matrix和你的输出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
Run Code Online (Sandbox Code Playgroud)


flo*_*del 5

有人建议我把我的评论变成答案。

在R控制台中,键入时head(m, 5),屏幕上显示的内容实际上是的结果print(head(m, 5))。因此,如果这是您希望输出的外观,请考虑使用该print函数,而不是cat在显示head和时显示tail对象:

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
Run Code Online (Sandbox Code Playgroud)

我发现@mrdwab的答案是一个非常优雅的解决方案。它没有显式使用print,而是返回一个列表。但是,当从R控制台调用他的函数并且未将输出分配给任何东西时,则将其输出到控制台(因此print隐式使用)。我希望这可以帮助您了解发生了什么。