How to Extract Multiples of a Number from a Vector

How to extract multiples of a number from a vector

For this you can use the modulo operator, i.e. %%. Take for example:

> 322%%8
[1] 2

which tells you that after dividing 322 by 8, 2 remains, i.e. 320 is exactly 40 times 8, leaving 2.

In you example we can use %% combined with subsetting to get the the multiples of 8. Remember that %% yields 0 for exact multiples of 8:

input = 1:1000
multiple_of_8 = (input %% 8) == 0
head(multiple_of_8)
[1] FALSE FALSE FALSE FALSE FALSE FALSE
length(multiple_of_8)
[1] 1000

also note that %% is a vectorized operation, i.e. of the left hand side is a vector, the result will also be a vector. The multiple_of_8 vector now contains 1000 logicals stating if that particular element of input is an exact multiple of 8. Using that logical vector to subset get's you the result you need:

input[multiple_of_8]
[1] 8 16 24 32 40 48 56 64 72 80 88 96 104 112 120
[16] 128 136 144 152 160 168 176 184 192 200 208 216 224 232 240
[31] 248 256 264 272 280 288 296 304 312 320 328 336 344 352 360
[46] 368 376 384 392 400 408 416 424 432 440 448 456 464 472 480
[61] 488 496 504 512 520 528 536 544 552 560 568 576 584 592 600
[76] 608 616 624 632 640 648 656 664 672 680 688 696 704 712 720
[91] 728 736 744 752 760 768 776 784 792 800 808 816 824 832 840
[106] 848 856 864 872 880 888 896 904 912 920 928 936 944 952 960
[121] 968 976 984 992 1000

or more compactly:

input[(input %% 8) == 0]

Trying to get a multiple of 3 and 5 in R

Try :

p2<-function(x){
if (x%%3==0){
return ("Multipleof3")
} else if (x%%5 == 0){
return("Multipleof5")
} else {
return(x)
}
}

p<-Vectorize(p2)

p(1:100)

# [1] "1" "2" "Multipleof3" "4" "Multipleof5" "Multipleof3" "7" "8"

Note : you can replace p2 by :

p2<-function(x){
return(ifelse(x%%3==0, "Multipleof3", ifelse(x%%5==0, "Multipleof5", x)))
}

Your function parameter should not be in the question but defined like this : function(x) and when you call the function you put your parameter (1:100) at the place of x. The Vectorize allow to create a new function : p which is the same as p2 but which is capable of handling vectors.

Use :

p2<-function(x){
return(ifelse(x%%3==0 & x%%5==0, "MultipleOfBoth", ifelse(x%%3==0, "Multipleof3", ifelse(x%%5==0, "Multipleof5", x))))
}

for handling multiple of 3 and 5.

Extracting numbers from vectors of strings

How about

# pattern is by finding a set of numbers in the start and capturing them
as.numeric(gsub("([0-9]+).*$", "\\1", years))

or

# pattern is to just remove _years_old
as.numeric(gsub(" years old", "", years))

or

# split by space, get the element in first index
as.numeric(sapply(strsplit(years, " "), "[[", 1))

Extract elements from a vector that are repeated n times (R)

We can use table if it is based on frequency

v1 <- table(col.vector)
as.integer(names(v1)[v1 == 3])
[1] 3 4

If it is based on repeating elements, use rleid

library(data.table)
v1 <- table(rleid(col.vector))
as.integer(names(v1)[v1 == 3])

finding multiples of a number in Python

If you're trying to find the first count multiples of m, something like this would work:

def multiples(m, count):
for i in range(count):
print(i*m)

Alternatively, you could do this with range:

def multiples(m, count):
for i in range(0,count*m,m):
print(i)

Note that both of these start the multiples at 0 - if you wanted to instead start at m, you'd need to offset it by that much:

range(m,(count+1)*m,m)

Extract multiple ranges from a numeric vector

You were on the right path, and left.open indeed helps, but rightmost.closed actually concerns only the last interval rather than the right "side" of each interval. Hence, we need to use left.open twice. As you yourself figured out, it looks like an optimal way to do that is

x[findInterval(x, v) %% 2 == 1 & findInterval(x, v, left.open = TRUE) %% 2 == 1]
# [1] 2 3 4 9 10 11 18 19

Clearly there are alternatives. E.g.,

fun <- function(x, v)
if(length(v) > 1) v[1] < x & x < v[2] | fun(x, v[-1:-2]) else FALSE
x[fun(x, v)]
# [1] 2 3 4 9 10 11 18 19

How to extract a specific text from a vector that is separated with multiple commas in R

What about the following?

library(tidyverse)

df %>%
mutate(col2 = str_split(col1, "\\s*,\\s*") %>%
map_chr(~ if (length(.x) %in% 1:2) {.x[length(.x)]}
else {.x[length(.x) - 1]}))

#> col1 col2
#> 1 book, pencil,eraser,pen eraser
#> 2 book,pen pen
#> 3 music,art,sport art
#> 4 apple, banana, kiwi, watermelon kiwi
#> 5 Earth, Mars, Jupiter Mars


Related Topics



Leave a reply



Submit