说明:
我有一个充满来自传感器网络的数据的目录。我想使用位于文件名中的每个传感器的序列号来创建id向量。 。
以下是一些示例文件名:
2017-07-18-32058-aqdata.csv
2017-07-18-32033-aqdata.csv
。
每个传感器的序列号位于时间戳之后,例如32058
或32033
。
以下是我目前正在阅读的数据:
## Load the necessary packages:
if (!require(plyr)){
install.packages('plyr')
library(plyr)
}
if (!require(dplyr)){
install.packages('dplyr')
library(dplyr)
}
## Define a function to read in a single file:
read.data <- function(file_path){
df <- read.csv(file_path, header=TRUE, stringsAsFactors=FALSE)
names(df)<-c("datetime", "co", "co2", "VOC", "RH","ugm3","temp")
df$datetime <- strptime(df$datetime, format="%Y-%m-%d %H:%M")
df$datetime <- as.POSIXct(df$datetime, format="%Y-%m-%d %H:%M:%S")
return(df)
}
## Assign object 'file_path' to my target directory:
file_path <-"~/my_directory/"
## Generate a list of files within this directory:
file_list <- list.files(path = file_path, pattern="\\.csv$", all.files=FALSE, full.names=TRUE, ignore.case=FALSE)
## Apply the data.read function to the list of files:
df_master <- dplyr::ldply(file_list, read.data)
df_master <- plyr::arrange(df_master, datetime)
如何利用每个文件名中的序列号在read.data()
函数中创建相应的ID向量?
以下是一些示例数据:
df_example <- structure(list(datetime = structure(c(1497296520, 1497296580, 1497296640, 1497296700, 1497296760, 1497296820), class = c("POSIXct", "POSIXt"), tzone = ""), co = c(0, 0, 0, 0, 0, 0), co2 = c(1118L, 1508L, 836L, 620L, 529L, 498L), VOC = c(62.1353, 59.7594, 59.1831, 57.9592, 56.4335, 53.6528), RH = c(51.45, 52.18, 50.72, 49.71, 49.21, 48.51), ugm3 = c(2.601, 1.061, 1.901, 1.481, 2.501, 3.261), temp = c(72.27, 72.35, 72.45, 72.55, 72.67, 72.77)), .Names = c("datetime", "co", "co2", "VOC", "RH", "ugm3", "temp"), row.names = c(NA, 6L), class = "data.frame")
提前致谢!
答案 0 :(得分:1)
这假设您的传感器编号都是5个以上的数字,这有助于避免与日期混淆。使用stringr
:
library(stringr)
read.data <- function(file_path){
df <- read.csv(file_path, header=TRUE, stringsAsFactors=FALSE)
names(df)<-c("datetime", "co", "co2", "VOC", "RH","ugm3","temp")
df$datetime <- strptime(df$datetime, format="%Y-%m-%d %H:%M")
df$datetime <- as.POSIXct(df$datetime, format="%Y-%m-%d %H:%M:%S")
# New code to pull in sensor number
df$sensor <- str_extract(file_path, "[0-9]{5,}")
return(df)
}