0

I believe I am making some basic mistake in indexing for a negative subset. As can be seen in the code, I first showcase the normal positive subsets followed by negative subsets for the same elements. My understanding was that in the latter, the value from the positive subset would have been removed and the data set would then showcase the entire data without those elements. Why is my approach wrong?

x<-list(a=list(10,12,14),b=c(3.14,2.81))

x[[c(1,1)]]

[1] 10

x[[c(2,1)]]

[1] 3.14

Omitting

x[[-c(1,1)]]

[1] 2.81

x[[-c(2,1)]]

Error in x[[-c(2, 1)]] : attempt to select more than one element in get1index

A. Suliman
  • 12,923
  • 5
  • 24
  • 37

1 Answers1

0

If you want to omit the first element of the first list, you'd do so like this:

x[[1]][-1]

[[1]]
[1] 12

[[2]]
[1] 14

And to omit the first element of the second list:

x[[2]][-1]

[1] 2.81

To apply the first subset back:

x$a <- x[[1]][-1]

You can also subset like this:

x$a[-1]

[[1]]
[1] 12

[[2]]
[1] 14
Mako212
  • 6,787
  • 1
  • 18
  • 37