'How to show first 10 rows and first 5 columns in R?

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

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


Solution 1:[1]

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)

Solution 2:[2]

Try something along the lines of

small[1:10, 1:5]

Solution 3:[3]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dan Adams
Solution 2 Oliver
Solution 3 Konrad