在R中组合头尾法 [英] combining head and tail methods in R

查看:206
本文介绍了在R中组合头尾法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在R程序包中使用头(d)和tail(d)方法 - 经常一个接一个地使用。所以我为两个函数写了一个简单的包装器:

  ht<  -  function(d,m = 5,n = m ){
#打印头和尾一起
cat(head - >,head(d,m),\\\
,-------- ,\\\
,tail - >,tail(d,n),\\\

}

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



某些背景...



数字很好:

  x<  -  1:100 
ht(x)

复杂:

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

和字符:

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

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

  m<  - 矩阵(1:10,20)
ht(m)

to:

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

我想保持矩阵结构, utils方法是 - 这可能吗?



最后(好吧,可能会有更多的错误,这只是我要做的)data.frames是一个混乱:

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

这会产生以下错误:

  head  - > cat(list(...),file,sep,fill,labels,append)中的错误:
参数2(类型'list')不能由'cat'处理

到目前为止的步骤:



由于utils方法在完成后保持矩阵整齐我尝试用以下编辑来解决问题:

  function(d,m = 5,n = m){ 
#打印头和尾一起
rb< - rbind(head(d,m),tail(d,n))
if(class(d)=='matrix' ){
len< - nrow(rb)
cat(head - >,rb [(1:m),],\\\
---,\\\
,tail - >,rb [((len-n):len),],\\\

}
else cat (head - >,rb [1,],\\\
,--------,\\\
,tail - >,rb [ ],\\\

}

哪个似乎没有做任何事情当我

  ht(df)

我从错误中猜出rs,这里有一些问题的猫(),但我无法弄清楚它是什么或如何解决它。



有人可以帮忙吗?

解决方案

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

  ht < -  function(d,m = 5,n = m){
#打印头和尾一起
列表(HEAD = head(d, m),TAIL = tail(d,n))
}

您的矩阵 data.frame

  ht(矩阵(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


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 is fine:

x <- 1:100
ht(x)

As is complex:

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

and character:

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

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?

解决方案

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

这篇关于在R中组合头尾法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆