R: selecting items matching criteria from a vector - r

I have a numeric vector in R, which consists of both negative and positive numbers. I want to separate the numbers in the list based on sign (ignoring zero for now), into two seperate lists:
a new vector containing only the negative numbers
another vector containing only the positive numbers
The documentation shows how to do this for selecting rows/columns/cells in a dataframe - but this dosen't work with vectors AFAICT.
How can it be done (without a for loop)?

It is done very easily (added check for NaN):
d <- c(1, -1, 3, -2, 0, NaN)
positives <- d[d>0 & !is.nan(d)]
negatives <- d[d<0 & !is.nan(d)]
If you want exclude both NA and NaN, is.na() returns true for both:
d <- c(1, -1, 3, -2, 0, NaN, NA)
positives <- d[d>0 & !is.na(d)]
negatives <- d[d<0 & !is.na(d)]

It can be done by using "square brackets".
A new vector is created which contains those values which are greater than zero. Since a comparison operator is used, it will denote values in Boolean. Hence square brackets are used to get the exact numeric value.
d_vector<-(1,2,3,-1,-2,-3)
new_vector<-d_vector>0
pos_vector<-d_vector[new_vector]
new1_vector<-d_vector<0
neg_vector<-d_vector[new1_vector]

purrrpackage includes some useful functions for filtering vectors:
library(purrr)
test_vector <- c(-5, 7, 0, 5, -8, 12, 1, 2, 3, -1, -2, -3, NA, Inf, -Inf, NaN)
positive_vector <- keep(test_vector, function(x) x > 0)
positive_vector
# [1] 7 5 12 1 2 3 Inf
negative_vector <- keep(test_vector, function(x) x < 0)
negative_vector
# [1] -5 -8 -1 -2 -3 -Inf
You can use also discard function

Related

Rounding numbers to the nearest values (with different intervals) in R

I want to round (or replace) numbers in a
a <- c(0.505, 1.555, 2.667, 53.850, 411.793)
to the nearest values in b:
b <- c(0, 5, 10, 50, 100, 200, 500)
The output will be this:
a_rnd <- c(0, 0, 5, 50, 500)
The logic is simple but I couldn't find any solution, because all the approaches I found require values in b have an equal interval!
How can I achieve this?
You can use sapply to loop over all values of a and use these indexes to extract the proper b values
b[sapply(a, function(x) which.min(abs(x - b)))]
#> [1] 0 0 5 50 500
This is a relatively simple approach:
b[apply(abs(outer(a, b, "-")), 1, which.min)]

Addition involving numeric(0) values

Suppose x is a real number, or a vector. i is valued-False. Then x[i] will return numeric(0). I would like to treat this as a real number 0, or integer 0, which are both fit for addition.
numeric(0) added to any real number will return numeric(0), whereas I wish to obtain the real number being added as the result. What can I do to convert the numeric (0) value? Thanks in advance!
It is only when we do the +, it is a problem. This can be avoided if we use sum
sum(numeric(0), 5)
#[1] 5
sum(numeric(0), 5, 10)
#[1] 15
Or if we need to use +, an easy option is to concatenate with 0, select the first element. If the element is numeric(0), that gets replaced by 0, for other cases, the first element remain intact
c(numeric(0), 0)[1]
#[1] 0
Using a small example
lst1 <- list(1, 3, numeric(0), 4, numeric(0))
out <- 0
for(i in seq_along(lst1)) {
out <- out + c(lst1[[i]], 0)[1]
}
out
#[1] 8
You can use max/min with 0 to get 0 as output when input is numeric(0).
x <- 1:10
max(x[FALSE], 0)
#[1] 0
min(x[FALSE], 0)
#[1] 0

R - How to plot a graph from bool values

So, I have a vector full of 1s and 0s. I need to plot a graph that starts at (0, 0) and rises by 1 for every 1 in the vector and dips by 1 for every 0 in the vector. For example if my vector is [ 1, 1, 1, 0, 1, 0, 1, 1 ] I should get something that looks like
I thought about creating another vector that would hold the sum of the first i elements of the original vector at index i (from the example: [ 1, 2, 3, 3, 4, 4, 5, 6 ]) but that would not account for the dips at 0s. Also, I cannot use loops to solve this.
I would convert the zeros to -1, add a zero at the very beginning to make sure it starts from [0,0] and then plot the cumulative sum:
#starting vec
myvec <- c(1, 1, 1, 0, 1, 0, 1, 1)
#convert 0 to -1
myvec[myvec == 0] <- -1
#add a zero at the beginning to make sure it starts from [0,0]
myvec <- c(0, myvec)
#plot cumulative sum
plot(cumsum(myvec), type = 'line')
#points(cumsum(myvec)) - if you also want the points on top of the line

R: How to find max length sequence between two values in a vector?

Let's say I want to find the longest length of consecutive numbers (excluding 0) in a sequence in R.
Example: (0,2,3,0,5) in this case it should return 2 .
The solution I came up with is as follows:
A1 <- c(1, 1, 0,1,1,1)
length =NULL
B<-rle(A1==0)
C<-B$lengths
D<-B$values
for(i in 1:length(C)){
if(D[i]==FALSE){length[i]=C[i]}
}
length <- length [!is.na(length )]
max(length)
[1] 3
How can I find the longest sequence of non-zero numbers in a vector in R?
We could use rle. A==0 output a logical index vector, rle computes the lengths and runs of values of adjacent elements that are the same for logical vector. Extract the lengths of values that are not '0' and get the max after removing the first and last elements to account for the maximum lengths of non-zero elements at the start or end of vector.
max(with(rle(A==0), lengths[-c(1, length(lengths))][
!values[-c(1, length(values))]]))
#[1] 2
Another example
A1 <- c(1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0,0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1)
max(with(rle(A1==0), lengths[-c(1, length(lengths))][
!values[-c(1, length(values))]]))
#[1] 4
Or
indx <- A1==0
max(with(rle(A1[which(indx)[1L] : tail(which(indx),1)]==0),
lengths[!values]))
#[1] 4
Update
Based on the new info, may be you can try,
A1 <- c(1, 1, 0,1,1,1)
max(with(rle(A1==0), lengths[!values]))
#[1] 3

R how many element satisfy a condition?

Is there a better way to count how many elements of a result satisfy a condition?
a <- c(1:5, 1:-3, 1, 2, 3, 4, 5)
b <- c(6:-8)
u <- a > b
length(u[u == TRUE])
## [1] 7
sum does this directly, counting the number of TRUE values in a logical vector:
sum(u, na.rm=TRUE)
And of course there is no need to construct u for this:
sum(a > b, na.rm=TRUE)
works just as well. sum will return NA by default if any of the values are NA. na.rm=TRUE ignores NA values in the sum (for logical or numeric).
If z consists of only TRUE or FALSE, then simply
length(which(z))
I've always used table for this:
a <- c(1:5, 1:-3, 1, 2, 3, 4, 5)
b <- c(6:-8)
table(a>b)
FALSE TRUE
8 7

Resources