如何将一个R脚本中的变量重用到另一个R脚本?

问题描述 投票:0回答:1

我的combs中有一个变量1_script.R(这是一个10行x 2列的矩阵)。我需要将这个变量重用到我的2_script.R中。我试图source()第一个脚本,但加载所有脚本,而我只需要变量combs

有帮助吗?

r variables
1个回答
0
投票

有几种方法可以做到这一点。如果您的模型需要很长时间才能进行训练并且您想在其他地方使用它,您可以使用R的RDS文件格式将模型保存为文件并在将来加载它而无需再次训练。

# Create a model and save it to a variable
model_lm <- lm(mpg ~ ., data = mtcars)

# Store the model as an RDS file
saveRDS(object = model_lm, file = "model_lm.rds")

# Load the model from file
model_lm2 <- readRDS("model_lm.rds")

# Use the model however you want
predict(object = model_lm2, newdata = mtcars)
© www.soinside.com 2019 - 2024. All rights reserved.