gf7
gf7

Reputation: 165

R display first 10 rows of data of certain columns?

In R - say I have a dataset (data) with the columns "mpg", "car", "cylinders". Is there a way to display the first 10 rows of data for just "mpg" and "car"?

head(data,10) works fine, but displays all 3 columns - I wasn't sure if there was a way to display less columns without actually subsetting?

Upvotes: 2

Views: 3451

Answers (2)

ThomasIsCoding
ThomasIsCoding

Reputation: 101337

Try

head(data[c("mpg","car")],10)

or

head(data,10)[c("mpg","car")]

Upvotes: 3

pascal
pascal

Reputation: 1085

To subset your data use [rows, columns]:

Try one of the following:

head(data,10)[,1:2]

head(data,10)[,c("mpg","car")]

Upvotes: 2

Related Questions