How to filter a range of values?

Asked

Viewed 952 times

-2

Hello, I have the following vector

cvfm_multr =c(-0.332674874454252, -0.571035468021242, -0.676476547114675, 
-0.205905929370678, -1.21249714617946, 0.121238932104978, 0.310664697843136, 
0.289295847166303, 0.901385446521735, -0.309833968746656, -0.520120952588181, 
-0.0844448594304967, -0.976745809271752, -0.776290144153204, 
-0.184885406756014, -0.142871746761931, -0.175140951381509, 0.332749367055349, 
-0.167877841085048, 0.107230989696453, -0.127068243131427, -0.00120467836000015, 
1.59289322570735, -0.6400676239236, -0.612602029728549, -0.356144790420798, 
-0.66509355840466, 1.53476069624802, 0.962117829477668, -0.427562685619974, 
-0.114092240669723, -0.331650872703247, -0.0173431852757382, 
0.86567182476856, 0.711623091670348, -1.09468476654499, -0.158506855954527, 
-1.70237480123871, -0.163480402302945, 0.0517622549279752, -0.109294284525413, 
-0.350460021495665, 0.219298274702737, 0.190722102718247, 0.110726445355878, 
-0.221639078065147, -0.517428478048071, -0.379759736754142, 0.0699013813961145, 
1.23580755652926, -0.0281845364141073, 0.201960906866412, -0.191733850513577, 
0.277780431314049, 0.580701797502621, -0.790961762259559, -0.219756263014785, 
-0.184715132549396, 0.881829463390016, -0.6803115881401, 0.124959699034149, 
0.0751321383998971, 0.468896090198052, 0.452941954147714, -0.666520346122582, 
-0.237228833451289, -0.64317134458994, 0.338099996025719, 0.526538079141663, 
-0.0253101778815715, -0.520702714708789, -0.567901145112475, 
-0.419837611936896, -0.0322619125499162, -0.567342409963063, 
0.531761169987938, 0.336789067570332, 0.559872327513158, 0.318234305460718, 
0.343336523615117, 0.364370327641974, 0.0384321672450789, -0.0835259307641959, 
-0.22011046914361, 0.0827311049463448, 0.40719656105038, 0.309143399962243, 
0.15679294379371, 0.152544711629328, -0.081211948077318, -0.495755077139742, 
-0.403715568049029, -0.53887533702925, 0.377960837971345, -0.735995532445575, 
-1.20478356453464, -0.420570823751769, -0.194825849817067, 0.899973198972796, 
-0.394500029331577, -0.390607741941133, -0.799640046659479, -0.136696830566131, 
-1.08260697697428, -0.775504714704594, -0.0943951030210549, 0.0634557770416648, 
-0.0835673542941062, -1.06544921804954, 0.34777180006679, 1.71539397340291, 
-0.48828193673235, 0.0608130661011709, -0.924989665276589, 1.39036703699581, 
0.365282814309555, -0.488301518289066, 0.0425165646309361, -0.203895771778374, 
1.16823456194618, 0.165954094089568, 0.862297170795701, -0.594635646041965, 
-0.841712458812268, 0.354909105061918, 1.22912612960163, 0.576274682635165, 
0.220185371582198, 0.484731181665675, -0.606971118367062, 0.104753738622828, 
-0.162020765207001, 0.782399135047303, -0.320697972907844, -0.0182458937948447, 
0.454188994918196, -0.405861663495054, 0.741532626912437, -1.03449500982114, 
-1.3738744326152, 0.43463836450904, -0.381059096385153, 0.352017248177628, )

want to remove values greater than 1.759594 and smaller than -1.759594, I am using the following function

vef6m_rp <-vef6m_multr[vef6m_multr<1.759594&vef6m_multr>(-1.759594)]

but not working vef6m_rp is equal to the original

  • 1

    The vector you present has no values outside this range. range(cvfm_multr) gives [1] -1.702375 1.715394.

3 answers

4

The vector cvfm_multr is not less than -1.759594 and not bigger than 1.759594.

However, supposing its vector was x = [-2, -1, 0, 1, 2], just follow the procedure below:

x = c(-2, -1, 0, 1, 2)

x[(x >= -1.759594) & (x <= 1.759594)]

3

As @William Vieira quoted in the comments, the crease defined in the question does not exist in the vector cvfm_multr. Anyway, here’s a solution with tidyverse. First of all, I create a data.frame to use its functions:

df_1 <- data.frame(cvfm_multr)

Now, what do you wish:

library(tidyverse)

df_2 <- df_1 %>% 
  filter(between(cvfm_multr, -1.759594 , 1.759594))

dplyr::between is a shortcut for logical operators <= and >=. Thus, you don’t have to write them down:

df_3 <- df_1 %>% 
  filter(cvfm_multr >= -1.759594, cvfm_multr <= 1.759594)

See also this question and this other, both related to their.

  • 1

    Honestly, there’s no point in creating a data.frame in order to use the tidyverse in this context. This slows down your code and adds unnecessary lines. The solution using R base would be the most correct way.

  • 1

    Yes, @Jdemello, you’re right. I only put a possible solution to the case.

2

Both of the above answers solve the problem presented. I am personally adept at using dplyr (included in tidyverse) for data processing.

However, I add the extended version of the @LLL response that might be useful in other contexts (e.g.: dplyr in a function or class context is sometimes Tricky and implies additional arguments)

So I leave the following code:

cvfm_multr[which(cvfm_multr > -1.759594 & cvfm_multr < 1.759594)]

The function which() returns a logical index with TRUE/1 for all locations where the condition is met. It’s equal to the @LLL function that does it automatically.

In this way it is visible the code of the Dice that can be used for other contexts. (example: I want to add a category only to remarks that meet certain conditions).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.