0

我在 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 个节点执行此操作,因此我尝试编写一个带有参数“节点”的函数来执行此操作。在本例中,我将输入 getNodeData(00D)。但是,当我这样做时,实际上创建数据框会出现问题。该函数运行但不创建任何新对象。有没有办法把这个脚本变成一个函数,以便我可以更轻松地执行 100 次?

4

1 回答 1

1

你可以做类似的事情

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,那么您可以将其设置为您的dat.vector.

于 2018-04-06T07:48:56.133 回答