我正在一个非常大的矩阵(50k +列和行)上并行应用此函数,并且需要对其进行优化以提高速度。输入是一个等级值数组,为数组中的每个等级值分配一个对应的值并返回该值数组。我有点不知道如何使它运行得更快。
my_function <- function(x) {
buckets <- round(length(x)*c(0.001075,0.001290,0.001720,0.002150,0.003010,0.004300,0.006449,0.008599,0.012899,0.019348,0.032247,0.053745,0.086637,0.140382,0.227449))
y <- numeric(length(x))
y[x<=buckets[1]] <- 311
y[x>buckets[1]&x<=buckets[2]] <- 40
y[x>buckets[2]&x<=buckets[3]] <- 30
y[x>buckets[3]&x<=buckets[4]] <- 20
y[x>buckets[4]&x<=buckets[5]] <- 15
y[x>buckets[5]&x<=buckets[6]] <- 10
y[x>buckets[6]&x<=buckets[7]] <- 8
y[x>buckets[7]&x<=buckets[8]] <- 6
y[x>buckets[8]&x<=buckets[9]] <- 5
y[x>buckets[9]&x<=buckets[10]] <- 4
y[x>buckets[10]&x<=buckets[11]] <- 3
y[x>buckets[11]&x<=buckets[12]] <- 2.4
y[x>buckets[12]&x<=buckets[13]] <- 2
y[x>buckets[13]&x<=buckets[14]] <- 1.8
y[x>buckets[14]&x<=buckets[15]] <- 1.6
y[x>buckets[15]] <- 0
return(y)
}