R 精确定义区间上两个向量的边界
给出了两个向量:R 精确定义区间上两个向量的边界,r,string,function,vector,character,R,String,Function,Vector,Character,给出了两个向量: vec_nums <- 1:20 vec_ltrs <- letters[1:10] 所需特性: 函数将通过y=传递的向量中的元素放置在position=中给出的位置,从左侧开始计数。所以position=3应该理解为“每三位”占3,6 该函数应处理数字字符串和因子向量,并返回有序因子 该函数应适用于因子、字符串和数字向量 如果向量y小于x中的插入次数,则函数应返回x的剩余部分,而不进行任何添加 建议结构 我设想这一结构的职能是: funMergeVectsByP
vec_nums <- 1:20
vec_ltrs <- letters[1:10]
所需特性:
y=
传递的向量中的元素放置在position=
中给出的位置,从左侧开始计数。所以position=3
应该理解为“每三位”占3,6y
小于x
中的插入次数,则函数应返回x
的剩余部分,而不进行任何添加funMergeVectsByPlace <- function(x,y position = 3) {
# Convert
vec_a <- as.character(x)
vec_b <- as.character(y)
# Missing part
# Combine two vectors
# Create ordered factor
vec_fac <- factor(vec_mrg,
# levels =
# I want the levels to reflect the order of elements in the vec_merg
)
# Return
return(vec_fac)
}
funMergeVectsByPlacevec\u mrg无循环解决方案:
funMergeVectsByPlace <- function( x, y, position )
{
n <- min( length(y)%/%(position-1), length(x) )
A <- rbind( matrix(head(y,n*(position-1)),position-1), head(x,n) )
rest <- c( x[-(1:n)], y[-(1:(n*(position-1)))] )
c(c(A),rest)
}
> library(microbenchmark)
> vec_nums <- 1:20
> vec_ltrs <- letters[1:10]
> microbenchmark(Lafortune = vec_mrg(vec_nums,vec_ltrs,3),
+ mra68 = funMergeVectsByPlace(vec_nums,vec_ltrs,3),
+ times .... [TRUNCATED]
Unit: microseconds
expr min lq mean median uq max neval
Lafortune 137.677 143.112 161.12006 146.734 153.980 2931.512 10000
mra68 77.443 81.067 92.13208 83.331 86.954 2718.204 10000
既不是vec\u mrg
也不是funMergeVectsByPlace
返回因子。如果其中包括因子(…)
,这两个函数都变慢了,但是funMergeVectsByPlace
仍然比vec_mrg
快。你自己已经尝试过的任何东西都要快。messy_vec和regex部分来自哪里?赋值?Pascal可能是,但我只想在这个阶段开始工作。@Heroka来自另一个关于排序messy字符串的讨论。我实际上可以更改它,因为它与手头的任务没有太大关系。@Konrad你意识到这不是一个代码编写服务吗?我想这里的很多人都喜欢谜题,但你自己在如何解决它而不是“做这个”方面的一些努力/想法可能不会出错。
vec_mrg <- c(vec_nums, vec_ltrs)
vec_mrg <- order(vec_mrg)
for (i in 1:length(vec_nums)) {
pos <- position
vec_nums[pos] <- vec_ltrs[i]
pos <- pos + pos
# i will be out of bounds and the way to move the other vector is missing
}
vec_mrg <- function(x,y,pos) {
res <- y
counter <- seq(floor(length(y)/(pos-1)))
for(i in counter) {
res <- append(res, x[i], seq(pos-1,by=pos, length.out=length(counter))[i])
}
res
}
vec_mrg(vec_nums, vec_ltrs, 3)
#[1] "a" "b" "1" "c" "d" "2" "e" "f" "3" "g" "h" "4" "i" "j"
#[15] "5"
funMergeVectsByPlace <- function( x, y, position )
{
n <- min( length(y)%/%(position-1), length(x) )
A <- rbind( matrix(head(y,n*(position-1)),position-1), head(x,n) )
rest <- c( x[-(1:n)], y[-(1:(n*(position-1)))] )
c(c(A),rest)
}
> library(microbenchmark)
> vec_nums <- 1:20
> vec_ltrs <- letters[1:10]
> microbenchmark(Lafortune = vec_mrg(vec_nums,vec_ltrs,3),
+ mra68 = funMergeVectsByPlace(vec_nums,vec_ltrs,3),
+ times .... [TRUNCATED]
Unit: microseconds
expr min lq mean median uq max neval
Lafortune 137.677 143.112 161.12006 146.734 153.980 2931.512 10000
mra68 77.443 81.067 92.13208 83.331 86.954 2718.204 10000
> vec_nums <- 1:2000
> vec_ltrs <- letters[rep(1:10,100)]
> microbenchmark(Lafortune = vec_mrg(vec_nums,vec_ltrs,3),
+ mra68 = funMergeVectsByPlace(vec_nums,vec_ltrs,3),
+ times .... [TRUNCATED]
Unit: milliseconds
expr min lq mean median uq max neval
Lafortune 32.993883 40.991796 63.758011 51.171020 90.122351 456.9748 1000
mra68 1.101865 1.489533 2.468496 1.751299 3.338881 230.0460 1000
> v1 <- vec_mrg(vec_nums,vec_ltrs,3)
> v2 <- funMergeVectsByPlace(vec_nums,vec_ltrs,3)
>
> v1 <- vec_mrg(1:20,letters[1:10],3)
> v2 <- funMergeVectsByPlace(1:20,letters[1:10],3)
> v1
[1] "a" "b" "1" "c" "d" "2" "e" "f" "3" "g" "h" "4" "i" "j" "5"
> v2
[1] "a" "b" "1" "c" "d" "2" "e" "f" "3" "g" "h" "4" "i" "j" "5" "6" "7" "8" "9" "10" "11" "12" "13" "14" "15" "16" "17" "18" "19" "20"
> identical(v1,v2[1:length(v1)])
[1] TRUE
>