Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in R Programming by (7.3k points)

How can I select the first 4 rows of a data.frame:

              Weight Response

1   Control     59      0.0

2 Treatment     90      0.8

3 Treatment     47      0.1

4 Treamment    106      0.1

5   Control     85      0.7

6 Treatment     73      0.6

7   Control     61      0.2

1 Answer

0 votes
by

You can use the head function that returns the first or last parts of a vector, matrix, table, data frame or function.

For example:

df <- data.frame(x=rnorm(100), y=rnorm(100))

> head(df,4)

           x          y

1 -0.1158253  1.6802782

2 -0.8149687  0.7795840

3  0.2422635  0.7132405

4 -1.4250984 -0.5428819

You can also use the index of the rows to print them as follows:

df[row.index, column.index]

For example:

df[1:4,]

           x          y

1 -0.1158253  1.6802782

2 -0.8149687  0.7795840

3  0.2422635  0.7132405

4 -1.4250984 -0.5428819

Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers

500 comments

94.2k users

Browse Categories

...