-1

Despite looking stumped by some "basic" r commands. This is the third time for me taking this course (Roger Peng's R programming on Coursera) because I end up lagging behind. I am a Stata user so am well versed in statistics.

Here are the problems I am having: First, how do I find the number of rows in a csv file. I found the answer because it is not a very big dataset so I just scrolled down.

The following code got me the first two rows:

read.csv ("hw1_data-3.csv", nrows = 2)

But I also need the last two rows and when I try,

read.csv ("hw1_data-3.csv", nrows = 152:153)

I get the error

Error: unexpected numeric constant in "read.csv ("hw1_data-3.csv", nrows 152"

Would love some help on this

Mark Miller
  • 12,483
  • 23
  • 78
  • 132

4 Answers4

2

First question,

how do I find the number of rows in a csv file

you can use nrow function

For example in mtcars dataset you can use

nrow(mtcars)

Which would return the row numbers

#[1] 32

I also need the last two rows

for that you can use tail function

tail(mtcars, 2)

#              mpg cyl  disp  hp drat   wt qsec vs am gear carb
#Maserati Bora 15.0   8  301 335 3.54 3.57 14.6  0  1    5    8
#Volvo 142E    21.4   4  121 109 4.11 2.78 18.6  1  1    4    2

where mtcars is your data frame and 2 is the number of rows you want to display.

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
1

We can use .N from data.table to get the number of rows after reading by fread

d1 <- fread("hw1_data-3.csv")
d1[, .N]

TO get the last two rows,

tail(d1, 2)
akrun
  • 874,273
  • 37
  • 540
  • 662
0

To find the number of rows in a csv file try this:

my.data <- read.csv("hw1_data-3.csv")
nrow(my.data)

Or:

dim(my.data)[1]

To extract the last two rows you can try this:

my.data[(nrow(my.data) - 1) : nrow(my.data), ]

A quick internet search located a blog post with a variety of methods for determining the number of rows in a csv file not yet mentioned here. I am a little reluctant to copy and paste those methods. So, for now, I am just providing the link to that blog:

http://www.r-bloggers.com/easy-way-of-determining-number-of-linesrecords-in-a-given-large-file-using-r/

Mark Miller
  • 12,483
  • 23
  • 78
  • 132
0

To find out how many rows are in your table, you need to read the whole table into R.

data <- read.csv("hw1_data-3.csv")

If you only need the last two rows from the dataset, use tail().

tail(data, n=2)

n specifies how many last rows to select.

nya
  • 2,138
  • 15
  • 29