【发布时间】:2020-08-12 06:20:14
【问题描述】:
我想将数据框(文件名)的一列的每一行中的值用作函数中的参数。此函数从具有匹配文件名的 .ods 文件中导入数据,并将信息提取到新的数据帧中。
我想将该函数应用于“文件名”表中的每个文件名,从而创建大约 50 个数据框。我想附加这些数据帧中的每一个,我想使用 rbind,最后得到一个数据帧。
我已经为一个文件名编写了它,但我正在努力弄清楚如何将其编写为一个在文件名列表中迭代重复,然后附加结果数据帧的函数。
我在下面写了一个例子,非常感谢任何帮助!
map <- data.frame(well = c("A01", "A02", "A03", "B01", "B02", "B03", "C01", "C02", "C03", "A01", "A02", "A03", "B01", "B02", "B03", "C01", "C02", "C03"),
plate = c(1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2))
filenames <- data.frame(filenames = c("file1", "file2", "file3"),
plate = c(1, 1, 2))
firstdatetime <- as.POSIXct("2020-03-26 07:56:20 GMT")
activefile <- as.vector(filenames[1,1])
data <- data.frame(read.ods(activefile))
###for the purposes of this example, a sample data file is created below
data <- data.frame(datetime = "2020-03-26 13:04:38 GMT", one = c(2,4,6), two = c(4, 6, 6), three = c(5, 5, 2))
###
plate <- as.numeric(filenames$plate[match(activefile, filenames$filenames)])
datetime <- as.POSIXct(data$datetime[1])
time <- as.numeric(difftime(strptime(datetime, "%Y-%m-%d %H:%M:%S"),
strptime(firstdatetime, "%Y-%m-%d %H:%M:%S")))
temp <- plate
df <- subset(map, plate == temp)
df$filename <- activefile
df$time <- time
a570 <- data.frame(data[,-1])
a570 <- as.vector(t(a570))
df$a570 <- a570
###
#then repeat this for each filename in 'filenames', adding each dataframe to the bottom of the one before. The final output would be one big data frame.
【问题讨论】:
-
您在此处发布的代码中有很多内容。我想确定一些事情:您有一个文件名向量(在数据框列中),并且 all 您需要做的就是将这些文件读入数据框并将它们行绑定,对吗?或者您还想在所有代码中完成什么?
-
我正在读入数据的真实文件比我给出的示例更复杂。我需要提取并重新格式化每个文件的几个部分来创建每个数据框。我试图在上面的例子中简化
标签: r function dataframe rbind