How to avois this error:only 0's may be mixed with negative subscripts? - r

I have this example:
x=c(NA, 2, -3, -4, -5, -6, -7, -8, -9, -10, -11, -2, 2, -14, -15, -16, -17, 2, -19, -20)
g= head(x[!is.na(x)], 13)
I want to exclude values that were already used for g.
y=x[-(head(x[!is.na(x)], 13))]
Is there a better way to do this?
I got this error:
Error in x[-(head(x[!is.na(x)], 13))] :
only 0's may be mixed with negative subscripts
any idea why?

You can use %in% to check which values are contained in g and negate every index which contains a value that is contained in your definition of g:
x[!(x %in% (head(x[!is.na(x)], 13))) | (1:length(x)) > which(cumsum(!is.na(x)) == 13)]
Your error occurs because you are mixing positive and negative indexes in your subsetting of x, which is not necessary because you do not have to work with the indexes rather than create a logical vector that gives you the place for every value not contained in g.
EDIT: I added a second logical vector which makes sure that values after the index of the 13th non-NA value cannot be removed, since they can never be contained in g (cause g is a subset of the first 13 non-NA-values of x). There may be an easier solution but this should do it..

It actually depends on the problem you are solving. If you want to delete the elements at positions 1-10 except NA, use:
pos = c(is.na(x[1:10]), 11:length(x))
y = x[-pos]
If you want to delete every element which takes the same value as the elements 2:10, then use:
setdiff(x,g)
As per the error is concerned, I guess in your case the elements in x are a mix of positive and negative values. For example,
letters[c(-1, -3, 5, 6, 7)]
Error: only 0's may mix with negative subscripts
A brilliant description is given here: https://www.stat.berkeley.edu/~nolan/stat133/Fall05/notes/RSubset.html

Related

In R, transform a vector with repeated values to a vector of single ordered values including the repetitions?

I have a vector x <- c(5, 5, 5, 3, 2, 2, 5, 5, 2). I need to get a new vector with the ordered distinct (but duplicates allowed) values:
y <- c(5, 3, 2, 5, 2)
I'm sure it's a trivial problem but I've been looking around and I don't see a simple way to do it.
Just out of the curiosity, I've tried to solve it without rle(x)$values:
x[x != c(x[-1], Inf)]
# [1] 5 3 2 5 2
Basically we are just comparing the original vector, and its' shifted to the left version, to create a subset index. So if the current element is equal to the next element, we return FALSE to remove this element from the initial vector.
555322552
|||||||||
55322552i
|||||||||
001101011
|||||||||
xx53x2x52
result: 53252
We always return the last element (hopefully, there are no NAs or NULLs).

How to find number of observation between first observation and observation with maximal value

I have a large data frame and I need a function to automate this search. Basically I want to find how many observations are between the first observation and the observation with maximal value.
Example:
x <- c(2, 1, 9, 3, 4, -6, 5, 11, 6, -7, -1)
Assuming that this is my data I want to count the number of data points between 2 and 11.
I need to do this in r.
Help is highly appreciated :D !!!
We can eithe
diff(which(x %in% c(2, max(x)))) -1
#[1] 6
Or substract the index of the max value (which.max) from the first value (+1 - not including the elements)
which.max(x) - x[1]

how do you count the number of results

Write a program that reads a series of numbers, ending with 0, and then tells you how
many numbers you have keyed in (other than the last 0). For example, if you keyed in
the numbers 5, -10, 50, 22, -945, 12, 0 it would output ‘You have entered 6 numbers.’.
doing my homework and can get this one to work
what stumps me is i understand adding the numbers to get the sum total but what do i call the number of numbers ...
thanks
Python has a very simple function that could be used here, string.count(). Given each number is separated by a comma, you can count the amount of commas to get the amount of numbers (not including the 0, which doesn't have a comma after it). An example of this in use would be
input = 5, -10, 50, 22, -945, 12, 0
Number_of_Numbers = input.count(',')

Identify same successively values in a vector in R

I'm new here and not really a pro in R. Hope you guys can help me out.
I've a vector ls and want to identify at least three same successively values in there.
Example:
ls <- c(1, -1, 1, -1, -1, -1, 1, 1, 1, -1, -1, -1, -1, -1, -1)
Now I want to identify the position in ls, where the minimum of three successively -1's does start. Here the output (the positions) would be :
[1] 4 10
Does any of you have an idea? Thanks.
You can use rle
r <- rle(ls)
(cumsum(r$lengths)-r$lengths+1)[r$value==-1 & r$length>=3]
#> [1] 4 10
The cummulative sum of run-lengths gives the endpoints of runs. Subtracting the lengths gives to position immediately before they start, so add one back. Subset the result for places where the value is -1 and the run-length is at least 3.

Absolute difference between a vector and a number in R

I'm new to R and I would be very grateful for an answer to my question:
I've got a vector: c(9, 11, 2, 6, 10) and the number 4 (or a vector c(4))
I want to generate a vector with the absolute difference between the first and the second one, which should look like this: c(5, 7, 2, 2, 6)
How do I do this? I can't get it to work with diff(), even after reading through the help (?diff()).
Any help is appreciated :)
x <- c(9, 11, 2, 6, 10)
abs(x - 4)
#[1] 5 7 2 2 6
abs finds the absolute value of a vector. '4' will be recycled when subtracted from x. If you have multiple values to be subtracted, they will also be recycled with a warning unless they are the same length as x.
You ran into problems with diff because it isn't designed for scalar subtraction (what you are attempting). It is better suited to finding the difference within a vector.

Resources