简体   繁体   English

维数不正确:从多个rdata文件中提取元素

[英]Incorrect number of dimensions: extracting elements from multiple rdata files

PROBLEM I have many .RData files in one folder and I want to extract the coordinates continued in each .rdata file. 问题我在一个文件夹中有许多.RData文件,我想提取每个.rdata文件中继续的坐标。 I'd also like to link the concomitant file name(use_hab) and datetime(dt) to each row of their respective coordinates. 我还想将伴随的文件name(use_hab)datetime(dt)到它们各自坐标的每一行。

CODE

file.namez<-list.files("C:/fitting/fitdata/7 27 2015") #name of files
#file.namez.rev<-file.namez[grep(".RData",file.namez)]

datastor<-data.frame(matrix(NA,length(file.namez),4))
names(datastor)<-c("use_hab",paste("B",1:3,sep=""))

allresults<-NULL

for(i in 1:length(file.namez))
  {
  datastor<-NULL
  print(file.namez[i])
  load(paste("C:/fitting/fitdata/7 27 2015/",file.namez[i], sep=""))
  use_hab <- as.character(as.data.frame(strsplit(file.namez[i],"_an"))[2,])# this line is used to remove unwanted parts of the file name
  use_hab <- gsub(".RData","", use_hab)
  datastor <- fitdata$coords
  datastor$use_hab <- use_hab
  datastor$dt <- fitdata$dt
  allresults <- rbind(allresults, datastor[,c(3,4,1,2)])
}

This is only result before the error message: 这仅是错误消息之前的结果:

[1] "fitdata_anw514_yr2008.RData"

ERROR 错误

Error in datastor[, c(3, 4, 1, 2)] : incorrect number of dimensions datastor [,c(3,4,1,2)]中的错误:维数不正确
In addition: Warning message: 另外:警告消息:
In datastor$use_hab <- use_hab : Coercing LHS to a list 在datastor $ use_hab <-use_hab中:将LHS强制为列表

QUESTION How am I getting the incorrect number of dimensions? 问题我如何得到不正确的尺寸数? Each file name should have 1098 coordinates and date time. 每个文件名应具有1098坐标和日期时间。 In total, 63 files x 1098 rows with 4 columns(filename, datetime, x, y). 总共63个文件x 1098行,共有4列(文件名,日期时间,x,y)。

The desired result is to have the file name as the first column, the date time as the second column, and the x and y coordinates as the third and fourth columns. 理想的结果是将文件名作为第一列,将日期时间作为第二列,并将x和y坐标作为第三列和第四列。

Replace 更换

  datastor <- fitdata$coords

with

 datastor$coords <- fitdata$coords

The error message Coercing LHS to a list is thrown when you try to access something with $ that does not support this. 当您尝试使用不支持此功能的$访问某些内容时,将引发错误消息“ Coercing LHS to a list datastor <- fitdata$coords changes datastor to the data type of fitdata$coords . datastor <- fitdata$coordsdatastor更改为fitdata$coords的数据类型。

Also, you'd change 另外,你会改变

allresults<-NULL
datastor<-NULL

to

allresults <- data.frame()
datastor <- data.frame()

but this may just my personal preference. 但这可能只是我个人的偏爱。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM