Home > Blockchain >  How to show first 10 rows and first 5 columns in R?
How to show first 10 rows and first 5 columns in R?

Time:03-10

I don't know how to use the head function to meet my needs

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

CodePudding user response:

You can provide multiple dimensions to the n argument of head().

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

Created on 2022-03-10 by the reprex package (v2.0.1)

CodePudding user response:

Try something along the lines of

small[1:10, 1:5]

CodePudding user response:

Colin Gillespie, in Efficient R programming, suggests adding the following functions to 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]

You will be able then run hh(mtcars) to obtain a quick summary below:

>> 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
  •  Tags:  
  • r
  • Related