Indexing/Slicing/Subsetting

Hello everyone,

I am still a beginner so sorry for that very basic question :slight_smile:
I am wondering why I am getting that error message in the second code line, while slicing x with c(TRUE, FALSE) works out.
I thought that, ,if one vector is shorter than the other, R will repeat the shorter vector. So I would expect x[TRUE, FALSE] to produce the same result as x[c(TRUE, FALSE)] does?

Thanks for your help!

x<-3:10

x[c(TRUE, FALSE)]
[1] 3 5 7 9

x[TRUE, FALSE]
error in x[TRUE, FALSE] :wrong number of dimensions

In the first code, you provide a single indexing vector of length 2, namely c(TRUE, FALSE). Yes, this vector will be recycled until all the elements of x are covered.

In the second code, you do not have single indexing vector. Rather, you have provided a row indexing vector, namely c(TRUE), and a column indexing vector, namely c(FALSE). R is confused and is wondering why you are supplying row and column indexes for a 1 dimensional object, i.e., the vector x.

You may wish to go through the swirl package for a tutorial on vectors.

install.packages("swirl")
library("swirl")
swirl()

Hope that helps,
Snehal

2 Likes

Yes, that helps, thank you!!

And I didn't know swirl yet, but it looks very good! The swirl() command does not seem to work - should it present me with course options? Or do I need to install courses from github first?

You can use the three lines of code in my initial response to install the package, load the library, and then launch the program.

Hmm, I can install the package and read the help function. But when I put in swirl() R says it can't find the function swirl. But Ill guess Ill find my way through it tomorrow:) Thank you very much for your advice!

Did you load the package as well with library("swirl")?

Yes, I did.. I get an error message saying that the package "RCurl" is not installed. I tried to install that but R says there is no package called "RCurl". I tried installing "curl" as I randomly saw it in my user library. But that didnt help either. Anyway, I can access the help function of the swirl-package and I simply need to find a way to start one of the courses :slight_smile:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.