Reputation: 25
Hi I'm learning R using Micheal J. Crawley's book on R 2nd ed. I have it a snag and I would like to know if the code I'm wrting is at cause or it is a bug that should be reported.
Starting page 36 section 2.6.1 is where I'm having that little problem.
Here is the command that I have input, the outputs are given as comments:
peas <- c(4, 7, 6, 5, 6, 7)
class(peas)
# [1] "numeric"*
length(peas)
# [1] 6*
peas[1:length(peas) %% 2 == 0]
# [1] 7 5 7*
Up to here it conformed with the book and the answer make sense in trying to extract the odds number.
Now if one tries to create a new vector "peas1" with the added numbers 4, 2, and 3. The odds number should be 7 5 7 and 3.
Here the sequence of commands
peas1 <- c(4, 7, 6, 5, 6, 7, 4, 2, 3)
length(peas1)
# [1] 9
peas1[1:length(peas1) %% 2 == 0]
#[1] 7 5 7 2
The answer is 7 5 7 and 2 .... Now that is wrong.. 2 is not an odd number!
Try again adding the number 9 at the end of a copy of the vector "peas1" as "peas2":
peas2 <- peas1
peas2[10] <- 9
peas2
# [1] 4 7 6 5 6 7 4 2 3 9*
peas2[1:length(peas2) %% 2 == 0]
# [1] 7 5 7 2 9*
See the result! 7 5 7 2 9
Now add 2 two more numbers to "peas2"
peas2[11] <- 2
peas2[12] <- 8
length(peas2)
# [1] 12
peas2
# [1] 4 7 6 5 6 7 4 2 3 9 2 8
peas2[1:length(peas2) %% 2 == 0]
# [1] 7 5 7 2 9 8
We try again and here the even number 2 and 8 appear in the sequence that should give only odd numbers according to the book and its command...
What is wrong the command?[Nothing only the understanding :-)] or there is a bug? [NO]
Great Answers you all, I was way out in the left field. THANKS
Upvotes: 1
Views: 304
Reputation: 263451
There is a distinction between the odd postions and the odd values. If you want the odd values use:
peas2[ peas2 %% 2 != 0 ]
If you want the odd positions:
peas2[ c(TRUE, FALSE) ] # argument recycling repeats the sequence along full length.
(I don't have a particularly high opinion of Crawley's book, at least in its first version. His explanations of R's syntax and object types in the first edition left a lot to be desired. It caused quite a few questions to Rhelp's mailing list. I didn't see any value in paying the high price for a second copy after the dissappointments caused by its first.)
Upvotes: 0
Reputation: 513
If you actually want to extract the odd numbers you can use:
odd_peas1 <- split(peas1,peas1%%2)
Of which the first element will be a string of the odd numbers.
Upvotes: 0
Reputation: 26
This example might be helpful for you:
> myMatrix <- c(1,2,3,4,5,6,7,8)
> myMatrix[1:length(myMatrix) %% 2 == 0]
[1] 2 4 6 8
> myMatrix <- c(1,3,5,7,9,11,13,15)
> myMatrix[1:length(myMatrix) %% 2 == 0]
[1] 3 7 11 15
>
So, basically, what you are doing is getting the even positions within myMatrix, not the even values of my matrix.
Upvotes: 1
Reputation: 2830
The example code peas[1:length(peas) %% 2 == 0]
extracts the even indexed elements of the peas array, it does not have anything to do with the value of the elements of the array.
Upvotes: 1