我在 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 次?