Ashwin
Ashwin

Reputation: 11

Splitting a vector/array in R

I would like to use R for performing an operation similar to merge sort. I would like to split my vector/array in to two parts. My input is present in a variable named inp.

> inp <- c(5,6,7,8,9,1,2,3,4)
> inplen <- length(inp)
> left <- inp[1:ceiling(inplen/2)]
> right <- inp[ceiling(inplen/2)+1:inplen]
> left
[1] 5 6 7 8 9
> right
[1]  1  2  3  4 NA NA NA NA NA
> length(left) 
[1] 5
> length(right)
[1] 9

Here you can see that though I split the vector in two halves the size of the right half is larger than the size of the left half. Also there are some entries in the right half that have the value NA. I am not able to figure out as to why the second vector created (called right) is having these extra entries.

Upvotes: 1

Views: 3756

Answers (3)

IRTFM
IRTFM

Reputation: 263311

You are running in to a (well-known) problem caused by a higher operator precedence for ":" than for "+":

ceiling(inplen/2)+1:inplen
[1]  6  7  8  9 10 11 12 13 14

NAs are being returned because your index exceeded the length of the vector.

Upvotes: 3

James
James

Reputation: 66834

You can use split for this, with cut to create the breakpoints:

split(inp,cut(seq(inplen),breaks=c(0,ceiling(inplen/2),inplen),labels=c("left","right")))
$left
[1] 5 6 7 8 9

$right
[1] 1 2 3 4

Upvotes: 1

csgillespie
csgillespie

Reputation: 60452

You're missing a bracket:

right = inp[(ceiling(inplen/2)+1):inplen]

To expand, suppose we have

1 + 1:3 

does this mean 1+(1:3) or (1+1):3. R interprets this as 1+(1:3) so when you type 1+1:3 you get:

1 + c(1,2,3)

which becomes

c(2,3,4)

Another common gotcha is:

R> x = 1:5
R> x[2:length(x)-1]
[1] 1 2 3 4

Instead of selecting elements 2 to 4, we have selected elements 1 to 4 by mistake.

Upvotes: 2

Related Questions