Skip to content

Odd behaviour of subset indexing (x:y).

2 messages · John Sorkin, Marc Schwartz

#
R 2.8.1
windows XP

I don't understand the output from x[iS+1:iE] produced by the code below:

x = c(1,2,3,4,5)
x
 [1] 1 2 3 4 5

 iS=2   #  start position
 iE=4   #  end position

[iS:iE]
[1] 2 3 4

# I don't understand the results of the command below. I would expect to see 3, 4, not 3, 4, 5, NA
x[iS+1:iE]
[1]  3  4  5 NA

Thanks,
John



John Sorkin M.D., Ph.D.
Chief, Biostatistics and Informatics
Baltimore VA Medical Center GRECC,
University of Maryland School of Medicine Claude D. Pepper OAIC,
University of Maryland Clinical Nutrition Research Unit, and
Baltimore VA Center Stroke of Excellence

University of Maryland School of Medicine
Division of Gerontology
Baltimore VA Medical Center
10 North Greene Street
GRECC (BT/18/GR)
Baltimore, MD 21201-1524

(Phone) 410-605-7119
(Fax) 410-605-7913 (Please call phone number above prior to faxing)
jsorkin at grecc.umaryland.edu
Confidentiality Statement:
This email message, including any attachments, is for th...{{dropped:6}}
#
on 01/20/2009 05:01 PM John Sorkin wrote:
Operator precedence.

Note:

# You are asking for indices 3:6 and of course x[6] does not exist
# hence the NA
# Equivalent to: iS + (1:iE)
[1] 3 4 5 6


# This is what you want
[1] 3 4


HTH,

Marc Schwartz