我想在不同的数据集上运行脚本。请参阅下面的说明性示例。我定义了一种参数,告诉R使用哪个数据集以及如何命名输出。现在,我希望有一个循环来运行所有数据集的脚本,即第一个par1 = 1,然后par2 = 1。你能给出一些建议吗?提前致谢。
library(MASS)
library(ggplot2)
library(xlsx)
# Parameters to define which dataset to use:
# if i want to use the dataset "beav1", i set par1 to 1
# if i want to use the dataset "beav2", i set par2 to 1
par1 = 1
par2 = 0
if(par1 == 1) {
df = beav1 # Load the dataset
Name = "beav1" # Dynamic parameter for plots and output files
par1 = 0
}
if(par2 == 1){
df = beav2 # Load the dataset
Name = "beav2" # Dynamic parameter for plots and output files
par2 = 0
}
# Make some data manipulation:
df = df[,2:3]
# Plot results:
ggplot(df, aes(x=time,temp)) +
geom_line() +
ggtitle(paste0(Name))
# Save results in xlsx format:
write.xlsx(df, paste0(Name, ".xlsx"))
答案 0 :(得分:0)
我认为您要找的是get
假设beav1
和beav2
是您要使用的数据集,那么您可以通过
data_names <- c("beav1", "beav2")
get(data_names[1]) # give you dataset beav1
get(data_names[2]) # give you dataset beav2
答案 1 :(得分:0)
我想我想出了如何实现它。我只是把那些&#34;参数&#34;转换为数据帧格式并通过循环运行。
data = data.frame("par1" = 0,"par2" = 0)
for(i in colnames(data)){
data[i] = 1
if(data[1] == 1){
df = beav1 # Load the dataset
Name = "beav1" # Dynamic parameter for plots and output files
}
if(data[2] == 1){
df = beav2 # Load the dataset
Name = "beav2" # Dynamic parameter for plots and output files
}
# Make some data manipulation:
df = df[,2:3]
# Plot results:
p=ggplot(df, aes(x=time,temp)) +
geom_line() +
ggtitle(paste0(Name)) +
ggsave(filename = paste0(Name, ".png"))
save(df,file=paste0(Name, ".Rda"))
data[i] = 0 # Set the values back to zero
}