R Split Numeric Vector at Position

R split numeric vector at position

An improvement would be:

splitAt <- function(x, pos) unname(split(x, cumsum(seq_along(x) %in% pos)))

which can now take a vector of positions:

splitAt(a, c(2, 4))
# [[1]]
# [1] 1
#
# [[2]]
# [1] 2 2
#
# [[3]]
# [1] 3

And it does behave properly (subjective) if pos <= 0 or pos >= length(x) in the sense that it returns the whole original vector in a single list item. If you'd like it to error out instead, use stopifnot at the top of the function.

How to split a character vector based on a numeric vector for positions

We can try substring:

substring(
vec,
c(1, split.points + 1),
c(split.points, nchar(vec))
)
# [1] "LAYRVCMTNEGHPWVSLVVQKTRLQ" "ISQDPSL"
# [3] "NYEYLPTMGLKSFIQASLALLFG" "KHSQAIVENRVGGVHTVGDSGAFQLGVQFLRAWHK"
# [5] "DARIVYIISSQKELHGLVFQDMGFTVYEYSVWDP" "KKLCMDPDILLNVVEQIPHGCVLVMGNIIDCKLTPSGWAKLMSM"

Split all values in column and store them in a single numeric vector

You already have the answer in your code since all the functions you are using are vectorised.

v <- as.numeric(na.omit(unlist(strsplit(df$col, ','))))
v
#[1] 2 6 10 5 10 1

Split named numeric vector into different elements in R

Here is one base R approach :

#Repeat each value in obj, obj number of times
res <- rep(obj, obj)
#Change all values to 1.
res[] <- 1
#Change the names
names(res) <- ave(names(res), names(res), FUN = function(x)
if(length(x) == 1) x else paste(x, seq_along(x), sep = '_'))
res

# V4_1 V4_2 V4_3 V10 V12 V15 V16_1 V16_2 V16_3 V16_4 V16_5 V16_6
# 1 1 1 1 1 1 1 1 1 1 1 1
# V20 V21 V22 V23 V36 V39 V40 V41 V42_1 V42_2 V42_3
# 1 1 1 1 1 1 1 1 1 1 1

Split vector by location

Both the previous answers rely on making lists/vectors for splitting that are the same length as the original x vector. This might be inefficient in the instance of a very long vector and can be avoided:

Map(function(i,j) x[i:j], a, cumsum(diff(c(a, length(x)+1))))

Split a numeric vector into continuous chunks in R

Here's another alternative:

vec <- c( 1, 2, 3, 4, 7, 8, 9, 10, 15, 16, 17 )
split(vec, cumsum(seq_along(vec) %in% (which(diff(vec)>1)+1)))
# $`0`
# [1] 1 2 3 4
#
# $`1`
# [1] 7 8 9 10
#
# $`2`
# [1] 15 16 17

Splitting numeric vectors in R

Here's one way using cut and split:

split(x, cut(x, c(-Inf, y, Inf)))
#$`(-Inf,3]`
#[1] 1 2 3
#
#$`(3,7]`
#[1] 5 7
#
#$`(7,10]`
#[1] 9 10
#
#$`(10, Inf]`
#[1] 12

Split string by position and numeric value in dplyr

Here's a verbose approach that might be helpful as you refine for edge cases.

library(tidyverse)

# combine splits @ 2 and @ 3
bind_rows(df1 %>% mutate(split_pos = 2),
df1 %>% mutate(split_pos = 3)) %>%

# calc features
mutate(num1 = str_sub(to_split, end = split_pos) %>% parse_number(),
num2 = str_sub(to_split, start = split_pos + 1) %>% parse_number(),
total = num1 + num2,
max = pmax(num1, num2)) %>%

# filter and pick best fit
filter(total %>% between(100, 250), max <= 150) %>%
arrange(id) %>%
group_by(id) %>%
slice_min(max) %>%
ungroup()



id to_split split_pos num1 num2 total max
<dbl> <chr> <dbl> <dbl> <dbl> <dbl> <dbl>
1 1 33118 2 33 118 151 118
2 2 37118 2 37 118 155 118
3 3 30121 2 30 121 151 121
4 4 41110 2 41 110 151 110
5 5 98121 2 98 121 219 121
6 6 101102 3 101 102 203 102
7 7 101110 3 101 110 211 110
8 8 10148 3 101 48 149 101
9 9 11121 3 111 21 132 111


Related Topics



Leave a reply



Submit