如何在自定义 R 函数中使用文本字符串作为变量
How to use textstring as variable in custom R function
我有以下问题:我有大量包含相同类型数据的 xlsx 文件,我想使用函数加载和操作它们,这样我就不必每次都复制粘贴新名称我想 运行 我的代码放在一个新文件上。我的代码如下(其中 Laerkevejen_1.xlsx 是文件名):
library(ggplot2)
library(dplyr)
library(readxl)
library(cowplot)
Laerkestien_1 <- read_excel("H:/Projekter/SagsAndSwells/Adresser og Alarmer/Laerkestien_1.xlsx")
Laerkestien_1$EventName <- as.factor(Laerkestien_1$EventName)
Laerkestien_1_NAs <- filter(Laerkestien_1,EventName=="Historical Power Outage" | EventName=="Historical Power Restore" |EventName=="ReverseCurrentStart"|EventName=="Meter Power Outage Event"|EventName=="Meter Power Restore Event"|EventName=="Meter Reverse Rotation"|EventName=="Phase 1 Power Loss"|EventName=="Phase 2 Power Loss"|EventName=="Phase 3 Power Loss"|EventName=="Phase1_2PowerLossStart"|EventName=="Phase2_3PowerLossStart"|EventName=="Phase1_3PowerLossStart")
Laerkestien_1_SagSwells <- filter(Laerkestien_1,EventName=="SagPhase1Stop" |EventName=="SagPhase2Stop" |EventName=="SagPhase3Stop" |EventName=="SwellPhase1Stop" |EventName=="SwellPhase2Stop" |EventName=="SwellPhase3Stop" )
Laerkestien_1_THDs <- filter(Laerkestien_1,EventName=="AverageVoltTHDPhase1"|EventName=="AverageVoltTHDPhase2"|EventName=="AverageVoltTHDPhase3"|EventName=="MaximumVoltTHDPhase1"|EventName=="MaximumVoltTHDPhase2"|EventName=="MaximumVoltTHDPhase3")
Laerkestien_1$LocalEventTime <- as.POSIXlt(Laerkestien_1$LocalEventTime)
Laerkestien_1_NAs$LocalEventTime <- as.POSIXlt(Laerkestien_1_NAs$LocalEventTime)
Laerkestien_1_SagSwells$LocalEventTime <- as.POSIXlt(Laerkestien_1_SagSwells$LocalEventTime)
Laerkestien_1_THDs$LocalEventTime <- as.POSIXlt(Laerkestien_1_THDs$LocalEventTime)
Laerkestien_1_NAs$MeasuredValue <- as.factor(Laerkestien_1_NAs$MeasuredValue)
Laerkestien_1_SagSwells$MeasuredValue <- as.factor(Laerkestien_1_SagSwells$MeasuredValue)
Laerkestien_1_THDs$MeasuredValue <- as.factor(Laerkestien_1_THDs$MeasuredValue)
ggplot(Laerkestien_1_SagSwells)+geom_point(aes(x=LocalEventTime,y=MeasuredValue,color=EventName))+scale_x_datetime(date_breaks = "1 month",date_labels = "%b %y")+scale_colour_manual(values=c("#999999", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7","#000000","red","cyan"))+theme(axis.text.x = element_text(angle=90))+ggtitle("Laerkevej_7_SagsSwells")
ggsave("Laerkestien_1_SagSwells.pdf",width=20,units="in")
ggplot(Laerkestien_1_THDs) + geom_point(aes(LocalEventTime,y=MeasuredValue,color=EventName))+scale_x_datetime(date_breaks = "1 month",date_labels = "%b %y")+scale_colour_manual(values=c("#F0E442", "#0072B2", "#D55E00", "#CC79A7","#000000","red","cyan"))+theme(axis.text.x = element_text(angle=90))+ggtitle("Laerkevej_7_THDs")
ggsave("Laerkestien_1_THDs.pdf",width=20,units="in")
ggplot(Laerkestien_1_NAs)+geom_point(aes(LocalEventTime,y=MeasuredValue,color=EventName))+scale_x_datetime(date_breaks = "1 month",date_labels = "%b %y")+scale_colour_manual(values = c("#999999", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7","#000000","red","cyan"))+theme(axis.text.x = element_text(angle=90))+ggtitle("Laerkevej_7_NAs")
ggsave("Laerkestien_1_NAs.pdf",width=20,units="in")
现在,我想在一个函数中完成所有这些,这样我就可以调用
Myfun(Laerkevejen_1)
或Myfun(Rolsoegaardvej_31)
,获取我需要的地块和东西。现在,我正在复制粘贴,并且可以使用,但是看起来很不优雅...
但是,我不确定如何实际创建函数,以便 'Laerkevejen_1' 作为变量名传递并像在我当前的代码中一样使用。
有谁知道如何顺利地做到这一点?
Laerkevejen_1
好像是数据,不是变量。您可以简单地在函数开头为数据集创建一个标准化对象:
FUN <- function(x){
# Here we use the x to read the data into the internal data object
temp_data <- read_excel(paste0("H:/Projekter/SagsAndSwells/Adresser og Alarmer/", x, ".xlsx"))
# from now on we replace the "Laerkevejen_1" in your code above with "temp_data"
temp_data$EventName <- as.factor(temp_data$EventName)
[...]
# Here we use the x again to get the string "Laerkejeven_1" into the filename
ggsave(paste0(x, "_SagSwells.pdf"), width = 20, units = "in")
[...]
}
然后用 FUN("Laerkevejen_1")
调用它并得到通常的结果。
我有以下问题:我有大量包含相同类型数据的 xlsx 文件,我想使用函数加载和操作它们,这样我就不必每次都复制粘贴新名称我想 运行 我的代码放在一个新文件上。我的代码如下(其中 Laerkevejen_1.xlsx 是文件名):
library(ggplot2)
library(dplyr)
library(readxl)
library(cowplot)
Laerkestien_1 <- read_excel("H:/Projekter/SagsAndSwells/Adresser og Alarmer/Laerkestien_1.xlsx")
Laerkestien_1$EventName <- as.factor(Laerkestien_1$EventName)
Laerkestien_1_NAs <- filter(Laerkestien_1,EventName=="Historical Power Outage" | EventName=="Historical Power Restore" |EventName=="ReverseCurrentStart"|EventName=="Meter Power Outage Event"|EventName=="Meter Power Restore Event"|EventName=="Meter Reverse Rotation"|EventName=="Phase 1 Power Loss"|EventName=="Phase 2 Power Loss"|EventName=="Phase 3 Power Loss"|EventName=="Phase1_2PowerLossStart"|EventName=="Phase2_3PowerLossStart"|EventName=="Phase1_3PowerLossStart")
Laerkestien_1_SagSwells <- filter(Laerkestien_1,EventName=="SagPhase1Stop" |EventName=="SagPhase2Stop" |EventName=="SagPhase3Stop" |EventName=="SwellPhase1Stop" |EventName=="SwellPhase2Stop" |EventName=="SwellPhase3Stop" )
Laerkestien_1_THDs <- filter(Laerkestien_1,EventName=="AverageVoltTHDPhase1"|EventName=="AverageVoltTHDPhase2"|EventName=="AverageVoltTHDPhase3"|EventName=="MaximumVoltTHDPhase1"|EventName=="MaximumVoltTHDPhase2"|EventName=="MaximumVoltTHDPhase3")
Laerkestien_1$LocalEventTime <- as.POSIXlt(Laerkestien_1$LocalEventTime)
Laerkestien_1_NAs$LocalEventTime <- as.POSIXlt(Laerkestien_1_NAs$LocalEventTime)
Laerkestien_1_SagSwells$LocalEventTime <- as.POSIXlt(Laerkestien_1_SagSwells$LocalEventTime)
Laerkestien_1_THDs$LocalEventTime <- as.POSIXlt(Laerkestien_1_THDs$LocalEventTime)
Laerkestien_1_NAs$MeasuredValue <- as.factor(Laerkestien_1_NAs$MeasuredValue)
Laerkestien_1_SagSwells$MeasuredValue <- as.factor(Laerkestien_1_SagSwells$MeasuredValue)
Laerkestien_1_THDs$MeasuredValue <- as.factor(Laerkestien_1_THDs$MeasuredValue)
ggplot(Laerkestien_1_SagSwells)+geom_point(aes(x=LocalEventTime,y=MeasuredValue,color=EventName))+scale_x_datetime(date_breaks = "1 month",date_labels = "%b %y")+scale_colour_manual(values=c("#999999", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7","#000000","red","cyan"))+theme(axis.text.x = element_text(angle=90))+ggtitle("Laerkevej_7_SagsSwells")
ggsave("Laerkestien_1_SagSwells.pdf",width=20,units="in")
ggplot(Laerkestien_1_THDs) + geom_point(aes(LocalEventTime,y=MeasuredValue,color=EventName))+scale_x_datetime(date_breaks = "1 month",date_labels = "%b %y")+scale_colour_manual(values=c("#F0E442", "#0072B2", "#D55E00", "#CC79A7","#000000","red","cyan"))+theme(axis.text.x = element_text(angle=90))+ggtitle("Laerkevej_7_THDs")
ggsave("Laerkestien_1_THDs.pdf",width=20,units="in")
ggplot(Laerkestien_1_NAs)+geom_point(aes(LocalEventTime,y=MeasuredValue,color=EventName))+scale_x_datetime(date_breaks = "1 month",date_labels = "%b %y")+scale_colour_manual(values = c("#999999", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7","#000000","red","cyan"))+theme(axis.text.x = element_text(angle=90))+ggtitle("Laerkevej_7_NAs")
ggsave("Laerkestien_1_NAs.pdf",width=20,units="in")
现在,我想在一个函数中完成所有这些,这样我就可以调用
Myfun(Laerkevejen_1)
或Myfun(Rolsoegaardvej_31)
,获取我需要的地块和东西。现在,我正在复制粘贴,并且可以使用,但是看起来很不优雅...
但是,我不确定如何实际创建函数,以便 'Laerkevejen_1' 作为变量名传递并像在我当前的代码中一样使用。
有谁知道如何顺利地做到这一点?
Laerkevejen_1
好像是数据,不是变量。您可以简单地在函数开头为数据集创建一个标准化对象:
FUN <- function(x){
# Here we use the x to read the data into the internal data object
temp_data <- read_excel(paste0("H:/Projekter/SagsAndSwells/Adresser og Alarmer/", x, ".xlsx"))
# from now on we replace the "Laerkevejen_1" in your code above with "temp_data"
temp_data$EventName <- as.factor(temp_data$EventName)
[...]
# Here we use the x again to get the string "Laerkejeven_1" into the filename
ggsave(paste0(x, "_SagSwells.pdf"), width = 20, units = "in")
[...]
}
然后用 FUN("Laerkevejen_1")
调用它并得到通常的结果。