R - values of x between 3 and 7 [duplicate] - r

This question already has answers here:
Check to see if a value is within a range?
(7 answers)
Closed 5 years ago.
I'm starting to learn R, as it's needed for work. I have never done statistical work, so I'm a bit lost.
I'm looking to get the value of x between two numbers.
So, for example, the range is 3:7 I need to print 4,5,6
I have tried
x <- 3:7
x[x>3 && x<7]
and
x <- 3
v <- 7
cbind(x, findInterval(x, v))
Any advice/guidelines

An option is between from data.table
x[data.table::between(x, 3, 7, incbounds = FALSE)]
#[1] 4 5 6

Related

How do I count conditionally in R [duplicate]

This question already has answers here:
Count observations greater than a particular value [duplicate]
(3 answers)
Closed 2 years ago.
I have a list
x <- c(1,2,3,4,5)
How do I count how many elements are over 3. So the output would be 2.
Take advantage of the fact logical are represented by 1 and 0
sum(1:5 > 3)
You can do like this:
x <- list(1, 2, 3, 4, 5)
sum( x > 3 )
#Output: [1] 2

Removing elements in R vector to correspond with NAs in another R vector [duplicate]

This question already has answers here:
Remove rows with all or some NAs (missing values) in data.frame
(18 answers)
Closed 3 years ago.
I have two vectors, say A and B
A <- c(1, 2, 3, 4, 5)
B <- c(6, NA, 8, 9, NA)
I would like to exclude elements in A corresponding to the elements of B which comprise NAs.
So, I am in need of an automatic way to remove indices 2 and 5 from both A and B, so that the length of both vectors is the same.
Use is.na
A[!is.na(B)]
#[1] 1 3 4
B[!is.na(B)]
#[1] 6 8 9
Something like
na.omit(cbind(A,B))

R Merge multiple vectors alternatively [duplicate]

This question already has an answer here:
Combine two equal length vectors alternating [duplicate]
(1 answer)
Closed 6 years ago.
I have 3 vectors
x<- 1:3
y<- 4:6
z<- 7:9
I want to do combine these three vectors into single vector k such that
k
[1] 1,4,7,2,5,8,3,6,9
I did this
k<-c()
for(i in 1:length(x)){
l<-c(x[i],y[i],z[i])
k<-c(k,l)
}
I want to avoid loops. Does anyone know how to do this without using a loop?
Thanks
We can rbind the vectors to a matrix and convert it to a vector with c
c(rbind(x,y,z))
#[1] 1 4 7 2 5 8 3 6 9

Delete vector elements in a vector [duplicate]

This question already has answers here:
How to tell what is in one vector and not another?
(6 answers)
Closed 5 years ago.
I try to do this in R:(for example)
let x = c(1,2,3,4,5,6,7,8) and y=c(1,2,8)
So
x[x!=y] = numeric(0) ????
I want to get as a result 3,4,5,6,7
Is there a practical way to do this?
Thanks
Use value matching %in% and remove the elements of x that are present in y
x[-which(x %in% y)]
#[1] 3 4 5 6 7

Sum from element 1 to current element in R [duplicate]

This question already has answers here:
Calculating cumulative sum for each row
(6 answers)
Closed 7 years ago.
I've come across the aggregate() function and things like seq_along(), but I'm not sure how to solve this yet:
For the following:
x
1
5
10
20
I'd like to get the following output:
y
1
6
16
36
It seemed to me that doing something like x[1:seq_along(x)] would do the trick but it seems not because seq_along(x) is a sequence rather than a number.
As mentioned by #DavidArenburg, you can use the cumsum function:
x <- c(1, 5, 10, 20)
cumsum(x)
[1] 1 6 16 36

Resources