我在R中编写了一个脚本,它将csv文件读取到数据帧,然后操纵该数据帧以创建一个新的数据帧。当前脚本如下:
#read in the csv file for a node
node00D = read.csv("00D.csv")
#create a new data frame with the specific measurement we want to decrease file size
node00D_smaller = node00D[node00D$sensor=="BMP180",]
#remove the columns that we don't need
keeps = c("node_id","timestamp","parameter","value")
node00D = node00D_smaller[keeps]
#fix row names
rownames(node00D) = 1:nrow(node00D)
#convert the timestamp column from a factor to a date and then truncate to the hour
node00D$timestamp = as.POSIXlt(node00D$timestamp)
node00D$timestamp = trunc(node00D$timestamp,"hour")
rm(keeps,node00D_smaller)
#get average temperature for each hour
library(plyr)
node00D$timestamp = as.character(node00D$timestamp)
node00D_means = ddply(node00D, .(timestamp), summarize,
mean=round(mean(value),2))
node00D$timestamp = as.POSIXlt(node00D$timestamp)
node00D_means$timestamp = as.POSIXlt(node00D_means$timestamp)
write.csv(node00D_means,"00D_Edit.csv")
#load lat long data
latlong = read.csv("Node.Lat.Lon.csv")
node00D_means$Node = "00D"
node00D_means = merge(node00D_means,latlong,by="Node")
我必须为多达100个节点执行此操作,因此我尝试使用参数'node'编写一个函数来执行此操作。在这个例子中,我将输入getNodeData(00D)。但是,当我这样做时,实际上存在创建数据帧的问题。该函数运行但不创建任何新对象。有没有办法将此脚本转换为函数,以便我可以更轻松地执行100次?
答案 0 :(得分:1)
您可以执行类似
的操作$#some
现在这个函数没有返回任何内容,但它保存了fun1 <- function(node.num){
### (1). Load the data
dat <- read.csv(paste0(node.num, ".csv"))
dat_smaller <- dat[dat$sensor=="BMP180",]
### (2). Here proceed with your code and substitute node00D(_smaller) by dat(_smaller) ###
# ------------------------------------------------------------------- #
### (3). Then define dat_mean and save the .csv
library(plyr)
dat$timestamp=as.character(dat$timestamp)
dat_means=ddply(dat,.(timestamp),summarize,mean=round(mean(value),2))
dat$timestamp=as.POSIXlt(dat$timestamp)
dat_means$timestamp=as.POSIXlt(dat_means$timestamp)
write.csv(dat_means,paste0(node.num, "_Edit.csv"))
### (4). And similarly for lat long
latlong=read.csv("Node.Lat.Lon.csv")
dat_means$Node=node.num
dat_means=merge(dat_means,latlong,by="Node")
}
个文件。但是,如果你想要它返回一些东西,例如.csv
,然后您可以在函数结束前添加行dat_means
。
<强>附录强>
现在要动态执行上述操作,您可以使用循环:
return(dat_means)
现在我不知道您的数据,但如果节点包含在### (1.) First, create an object containing all your nodes, e.g.
nodes.vector <- c("00D", ...)
### (2.) Run a loop, or use one of the apply functions
for(k in seq_along(nodes.vector)){
fun1(nodes.vector[k])
}
# Or
sapply(nodes.vector, fun1)
中,那么您可以将其设置为latlong$Node
。