R语言 托盘兼容性计算
Posted 基督徒Isaac
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了R语言 托盘兼容性计算相关的知识,希望对你有一定的参考价值。
# fun0----
fun0 <- function(df = data.frame(x = rep(1:3, each = 3),
y = rep(1:3, times =3)),
k1 = 1, k2 = 1)
X = df[order(df[,1], df[,2]),]
n = NROW(X)
Y = c()
for (i in 1:(n-1))
Y[[i]] = X[i,]
for (j in 1:(n-i))
a = X[i+j,1] - X[i,1]
b = X[i+j,2] - X[i,2]
if (a <= k1 &
b >= 0 &
b <= k2)
Y[[i]] = rbind(Y[[i]], X[i+j,])
return(Y)
# fun_index ----
fun_index <- function(df = data.frame(x = rep(1:3, each = 3),
y = rep(1:3, times = 3)),
k1 = 1, k2 = 1)
X = df[order(df[,1], df[,2]),]
n = NROW(X)
index = c()
# 1:n-1 × 1:(n-1) √
for (i in 1:(n-1))
index[[i]] = i
for (j in 1:(n-i))
a = X[i+j,1] - X[i,1]
b = X[i+j,2] - X[i,2]
if (a <= k1 &
b >= 0 &
b <= k2)
index[[i]] = rbind(index[[i]], i+j)
return(index)
# fun1----
fun1 <-
function(df = data.frame(x = rep(1:3, each = 3),
y = rep(1:3, times =3)),
k1 = 1, k2 = 1)
X = df[order(df[,1], df[,2]),]
n = NROW(X)
Y = c()
index = c()
for (i in 1:(n-1))
Y[[i]] = X[i,]
index[[i]] = i
for (j in 1:(n-i))
a = X[i+j,1] - X[i,1]
b = X[i+j,2] - X[i,2]
if (a <= k1 &
b >= 0 &
b <= k2)
Y[[i]] = rbind(Y[[i]], X[i+j,])
index[[i]] = rbind(index[[i]], i+j)
out = list(Index = index, Ans = Y)
return(out)
# fun2 ----
fun2 <- function(Ans = fun1()[[2]],
k = 0.1)
n = length(Ans)
tuopan = c()
for (i in 1:n)
tuopan = rbind(tuopan,
data.frame(
x = max(guige_Y[[2]][[i]][,1]) + k,
y = max(guige_Y[[2]][[i]][,2]) + k)
)
tuopan_result = dplyr::distinct(tuopan)
return(tuopan_result)
# debug ----
library(tidyverse)
fun_index()
fun0()
fun1()
Y <- fun1()
Index <- Y$Index
Ans <- Y$Ans
Ans[[1]] + 0.5 # 同时对行处理
Ans[[1]] + c(0.5, 10) # 第一行同时+0.5,第二行同时+10
Y[2] # length 1 指Ans或Index列表[]
Y[[2]] # length n-1 指列表里的列表[[]],等同于Y$Ans,也等同于Y[2][[1]],即:
# Y$Ans = Y[[2]] = Y[2][[1]]
Y[[2]][1] # 筛选列表里的列表 [[]],里面的列表[]
Y[[2]][[1]] # 筛选列表里的列表 [[]],里面的列表里的数据框[[]]
# Y[]是寻找列表的方法
# Y[[]][[]] 是寻找列表里你想要的结果的方法
Y[[2]][[1]][1] # 筛选数据框[[]],抽出数据框的一部分
Y[[2]][[1]][[1]] # 抽出数据框里的列向量
Y[[2]][[1]][[1]][1] # 根据索引位置抽出列向量里的值
Y = fun1()
Ans <- Y$Ans
Ans %>% last() # 最后一个列表
Ans[[1]] %>% last() # 某个数据框的最后一列(不是行)
Ans[[1]] %>% tail(1) # 最后一行
Ans[[1]][Ans[[1]] %>% NROW(),] # 同上
# real ----
library(tidyverse)
guige <- readxl::read_excel("E:/guige.xlsx", sheet = "Sheet1")
guige_Y <- fun1(guige, k1 = 80, k2 = 80)
guige_tuopan <- fun2(guige_Y[[2]], k = 20)
guige_tuopan %>% View()
write.csv(guige_tuopan, "e:guige_tuopan.csv")
guige_Y[[2]][1:3]
guige_tuopan %>% head(3)
以上是关于R语言 托盘兼容性计算的主要内容,如果未能解决你的问题,请参考以下文章