2
I would like that v2
only those values which are greater than x
. It even comes close, but creates a vector with logical elements. I wish they were the elements of v1
.
v1 <-c (1500, 1600, 1700, 1800)
x <- 1600
v2 <- v1 > x
2
I would like that v2
only those values which are greater than x
. It even comes close, but creates a vector with logical elements. I wish they were the elements of v1
.
v1 <-c (1500, 1600, 1700, 1800)
x <- 1600
v2 <- v1 > x
5
Without appealing to a ready-made function, you can simply use a conditional within a for loop as in the example below:
v1=c(1500,1600,1700,1800)
v2=c()
for (i in v1) {
if (i>1600) {
v2=c(v2,i)
}
}
print(v2)
It is inefficient and takes a lot of syntax, but you better understand what the computer is doing. Pre-ready solutions include the use of functions. An example is the function Filter
:
v1=c(1500,1600,1700,1800)
v2=Filter(function(x) x>1600, v1)
print(v2)
Or, in an even simpler solution suggested in the comment by another user:
v2=v1[v1 > x]
print(v2)
In the Filter
is v1
and no v
.
thanks @Noisy . Fixed
Browser other questions tagged r
You are not signed in. Login or sign up in order to post.
v1[v1 > x]
orsubset(x = v1, v1 > x)
– neves