如何在R中显示前10行和前5列

  • 本文关键字:10行 5列 显示 r
  • 更新时间 :
  • 英文 :


我不知道如何使用head函数来满足我的需求

small<- "Chicago_small.xlsx"
head(small, n=10)

您可以为head()n参数提供多个维度。

head(mtcars, c(3, 2))
#>                mpg cyl
#> Mazda RX4     21.0   6
#> Mazda RX4 Wag 21.0   6
#> Datsun 710    22.8   4

创建于2022-03-10由reprex包(v2.0.1(

尝试一些类似的方法

small[1:10, 1:5]

Colin Gillespie在高效R编程中建议向RProfile添加以下函数:

# ht == headtail
# Show the first 6 rows & last 6 rows of a data frame
ht = function(d, n=6) rbind(head(d, n), tail(d, n))
# Show the first 5 rows & first 5 columns of a data frame
hh = function(d) d[1:5, 1:5]

然后,您将能够运行hh(mtcars)以获得以下快速摘要:

>> hh(mtcars)
mpg cyl disp  hp drat
Mazda RX4         21.0   6  160 110 3.90
Mazda RX4 Wag     21.0   6  160 110 3.90
Datsun 710        22.8   4  108  93 3.85
Hornet 4 Drive    21.4   6  258 110 3.08
Hornet Sportabout 18.7   8  360 175 3.15

最新更新