使用 for 循环将 .RData 文件中的项目提取到空向量中?
Using for loop to extract items from .RData files into an empty vector?
我有一系列测试,每个测试都有自己的 .RData 文件。我试图在 R 中使用 for 循环从每个文件中提取特定变量。我目前的代码是:
setwd("C:/Users/c7271616/test files directory/") #File where test data files are
schedule <- list.files(pattern = ".RData") #list of files
p_inputs<- vector(mode = "numeric", length = length(schedule)) # empty vector to store results
for (i in 1:length(schedule)){
load(paste(getwd(),"/",schedule[i],sep = ""))
p <- as.numeric(P1)
p_inputs[i] <- p
}
p_inputs
结果是一个向量,其中前 n 项为 0,然后在几乎为空的向量末尾附加了一个 P 值。
0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000
0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000
0.000 0.000 6437.887
如何更改代码以便将“p”值存储在空向量中?我知道循环正在遍历所有文件,因为我在循环中试验了 print(p)
并且它列出了所有值但不将它们存储在向量中。
我已经设法通过使用像这样的计数器变量来解决这个问题:
setwd("C:/Users/c7271616/OneDrive - Leeds Beckett University/QUB Data/Gowbusk Farm/QUB Summarys/")
schedule <- list.files(pattern = ".RData")
#p_inputs <- data.frame(mode = "numeric", length = length(schedule))
p_inputs<- vector(mode = "numeric", length = length(schedule))
test_ids<- vector(mode = "character", length = length(schedule))
counter <- 1
for (i in 1:length(schedule)){
load(paste(getwd(),"/",schedule[i],sep = ""))
p <- P1
p_inputs[counter] <- p
test_id <- paste(test_date, duration)
test_ids[counter] <- test_id
counter <- counter +1
}
p_inputs
我有一系列测试,每个测试都有自己的 .RData 文件。我试图在 R 中使用 for 循环从每个文件中提取特定变量。我目前的代码是:
setwd("C:/Users/c7271616/test files directory/") #File where test data files are
schedule <- list.files(pattern = ".RData") #list of files
p_inputs<- vector(mode = "numeric", length = length(schedule)) # empty vector to store results
for (i in 1:length(schedule)){
load(paste(getwd(),"/",schedule[i],sep = ""))
p <- as.numeric(P1)
p_inputs[i] <- p
}
p_inputs
结果是一个向量,其中前 n 项为 0,然后在几乎为空的向量末尾附加了一个 P 值。
0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000
0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000
0.000 0.000 6437.887
如何更改代码以便将“p”值存储在空向量中?我知道循环正在遍历所有文件,因为我在循环中试验了 print(p)
并且它列出了所有值但不将它们存储在向量中。
我已经设法通过使用像这样的计数器变量来解决这个问题:
setwd("C:/Users/c7271616/OneDrive - Leeds Beckett University/QUB Data/Gowbusk Farm/QUB Summarys/")
schedule <- list.files(pattern = ".RData")
#p_inputs <- data.frame(mode = "numeric", length = length(schedule))
p_inputs<- vector(mode = "numeric", length = length(schedule))
test_ids<- vector(mode = "character", length = length(schedule))
counter <- 1
for (i in 1:length(schedule)){
load(paste(getwd(),"/",schedule[i],sep = ""))
p <- P1
p_inputs[counter] <- p
test_id <- paste(test_date, duration)
test_ids[counter] <- test_id
counter <- counter +1
}
p_inputs