user3553260
user3553260

Reputation: 721

How to print an certain amount of rows from a read.table in R Studio

da=read.table("m-ibm6708.txt",header=T)  #<== Load data with header

#<== Check dimension of the data (row = sample size, col = variables)
###############502 rows, col 1 = date, col 2 = ibm, col 3 = sprtn
#<== Print out the first 6 rows of the data object "da".
printrows <- da[1:6]
printrows    

The print rows didn't work. I tried a bunch of things. I think it might use cbind. da is a big table, but I only need the first 6 rows displayed.

Upvotes: 2

Views: 33130

Answers (2)

Alauddin Sabari
Alauddin Sabari

Reputation: 19

Looking like more advance selection then->

  • table_name[c(2:6, 9, 120), ]

note: from 2 to 6 rows | then 9 no: | then 120 no: row. in this way you can select a specific range as well as specific numbers of rows (or columns).

if you want to do this for columns just put a comma before like table_name[ ,c(2:6, 9, 120) ] before comma emptly means you selected all rows.

Upvotes: 1

Mark
Mark

Reputation: 4537

As jdharrison said, head(da,6) will work - alternatively you can use the indices to print the top 6 rows da[1:6,]

When using the indices notation remember it's [rows,columns] and you must include the comma if you have a data.frame or matrix - i.e. [1:6,] for the first six rows or [,1:6] for the first six columns.

Upvotes: 6

Related Questions