0

Want to read rows 11-20 from a dataset.. tried few things but couldn't get what I had in mind. There are 44 rows in the dataset(and the header).

> dim(read.table("smoking.dat",header=T))
[1] 44  6

From the documentation seemed like the skip argument(skip=10), combined with nrow=10 would give me rows 11-20.

 > read.table("smoking.dat",nrows=20,header=T)
   STATE   CIG BLAD  LUNG  KID LEUK
1     AL 18.20 2.90 17.05 1.59 6.15
2     AZ 25.82 3.52 19.80 2.75 6.61
3     AR 18.24 2.99 15.98 2.02 6.94
4     CA 28.60 4.46 22.07 2.66 7.06
5     CT 31.10 5.11 22.83 3.35 7.20
6     DE 33.60 4.78 24.55 3.36 6.45
7     DC 40.46 5.60 27.27 3.13 7.08
8     FL 28.27 4.46 23.57 2.41 6.07
9     ID 20.10 3.08 13.58 2.46 6.62
10    IL 27.91 4.75 22.80 2.95 7.27
11    IN 26.18 4.09 20.30 2.81 7.00
12    IO 22.12 4.23 16.59 2.90 7.69
13    KS 21.84 2.91 16.84 2.88 7.42
14    KY 23.44 2.86 17.71 2.13 6.41
15    LA 21.58 4.65 25.45 2.30 6.71
16    ME 28.92 4.79 20.94 3.22 6.24
17    MD 25.91 5.21 26.48 2.85 6.81
18    MA 26.92 4.69 22.04 3.03 6.89
19    MI 24.96 5.27 22.72 2.97 6.91
20    MN 22.06 3.72 14.20 3.54 8.28
> read.table("smoking.dat",nrows=10,skip=10)
   V1    V2   V3    V4   V5   V6
1  AL 18.20 2.90 17.05 1.59 6.15
2  AZ 25.82 3.52 19.80 2.75 6.61
3  AR 18.24 2.99 15.98 2.02 6.94
4  CA 28.60 4.46 22.07 2.66 7.06
5  CT 31.10 5.11 22.83 3.35 7.20
6  DE 33.60 4.78 24.55 3.36 6.45
7  DC 40.46 5.60 27.27 3.13 7.08
8  FL 28.27 4.46 23.57 2.41 6.07
9  ID 20.10 3.08 13.58 2.46 6.62
10 IL 27.91 4.75 22.80 2.95 7.27

The argument skip, only seems to have effect on the header. Can we use read.table() to only read lines 11-20. Thx

Bhail
  • 385
  • 1
  • 2
  • 18
  • Duplicate of http://stackoverflow.com/questions/23543825/r-read-table-how-to-get-header-and-skip-lines? – NPE Aug 29 '15 at 20:34

1 Answers1

1

I'm not getting the same behavior. (And I would have imagined that you would add 1 to the number of datalines you wanted to leave out.)

txt <- "   STATE   CIG BLAD  LUNG  KID LEUK
1     AL 18.20 2.90 17.05 1.59 6.15
2     AZ 25.82 3.52 19.80 2.75 6.61
3     AR 18.24 2.99 15.98 2.02 6.94
4     CA 28.60 4.46 22.07 2.66 7.06
5     CT 31.10 5.11 22.83 3.35 7.20
6     DE 33.60 4.78 24.55 3.36 6.45
7     DC 40.46 5.60 27.27 3.13 7.08
8     FL 28.27 4.46 23.57 2.41 6.07
9     ID 20.10 3.08 13.58 2.46 6.62
10    IL 27.91 4.75 22.80 2.95 7.27
11    IN 26.18 4.09 20.30 2.81 7.00
12    IO 22.12 4.23 16.59 2.90 7.69
13    KS 21.84 2.91 16.84 2.88 7.42
14    KY 23.44 2.86 17.71 2.13 6.41
15    LA 21.58 4.65 25.45 2.30 6.71
16    ME 28.92 4.79 20.94 3.22 6.24
17    MD 25.91 5.21 26.48 2.85 6.81
18    MA 26.92 4.69 22.04 3.03 6.89
19    MI 24.96 5.27 22.72 2.97 6.91
20    MN 22.06 3.72 14.20 3.54 8.28"

> read.table(text=txt,nrows=10,skip=11)
   V1 V2    V3   V4    V5   V6   V7
1  11 IN 26.18 4.09 20.30 2.81 7.00
2  12 IO 22.12 4.23 16.59 2.90 7.69
3  13 KS 21.84 2.91 16.84 2.88 7.42
4  14 KY 23.44 2.86 17.71 2.13 6.41
5  15 LA 21.58 4.65 25.45 2.30 6.71
6  16 ME 28.92 4.79 20.94 3.22 6.24
7  17 MD 25.91 5.21 26.48 2.85 6.81
8  18 MA 26.92 4.69 22.04 3.03 6.89
9  19 MI 24.96 5.27 22.72 2.97 6.91
10 20 MN 22.06 3.72 14.20 3.54 8.28
IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • read.table("smoking.dat",nrows=20,header=T)[11:20,] # this works but not skip=..., wonder whats going on. – Bhail Aug 29 '15 at 20:52
  • 1
    If the header is the first row and you want to keep it, then there is no single step method. You would need to construct an intermediate text object. – IRTFM Aug 29 '15 at 21:00