函数内的write.table应用于数据帧列表覆盖输出 [英] write.table inside a function applied to a list of data frames overwrite outputs

查看:119
本文介绍了函数内的write.table应用于数据帧列表覆盖输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我几乎完成了混乱的代码,以便将几种统计方法/测试应用于来自不同流域的11个数据帧,其理化参数为变量.我达到了目标,但我需要执行此功能. 因此,我首先创建了一个计算相关性的函数,并将结果保存为.txt表和.pdf图像. 一次将功能运行到一个数据框时,它的效果很好(为此,您应使用read.table分别导入每个数据框,该代码未在下面的代码中编写). 如我所愿,它列出了11个数据框,并使用lapply来对每个数据框运行该函数.从某种意义上说,它的工作原理是给我一个列表(corr),其中包含每个数据帧的相关结果.

I almost finish a messy code to apply several statistical methods/test to 11 data frames from different watersheds with physico-chemical parameters as variables. I reach the goal, but I need to do this functional. So to start i made a function to compute correlation, and save the results as .txt tables and .pdf images. It works great when run the function to one dataframe at the time (for that you should import each dataframe separately using read.table, which is not written in the code below). As i want it functional, made a list of the 11 dataframes and use lapply to run the function to each one. It works in the sense that gives me one list (corr) containing the correlation results of each dataframe.

问题来了:

  1. 具有每个数据帧相关结果的列表cor看起来具有值而不是数据帧,因此我不知道如何访问或保存它们(请参阅环境/数据"窗口中的corr列表).好吧,直到这里,至少看起来相关结果存在于某个地方.
  2. 第二个问题是,当我运行corr<-lapply(PQ_data, cor_PQ)时,该行使用计算出的原始数据帧名称的一部分将输出保存为表(.txt)和图像(.pdf)(例如, PQ_data"AgIX_E_PQ",因此cor_PQ(PQ_data[["AgIX_E_PQ"]]的表和图应分别获得名称"mCorAgIX_E_PQ.txt"和"CorAgIX_E_PQ.pdf",仅获得一个输出(mCorX [[I]].txt和CorX [ [i] .pdf)与最后一个数据帧相关结果.也就是说,每个数据帧相关结果的表和图像都将被覆盖到此通用mCorX [[I]].txt,CorX [[i]].pdf文件中.
  1. The list cor with correlation results for each dataframe looks like has values instead of data frames, so i dont know how to access or save them (see the corr list in the Environment/Data window). Well, until here, at least looks like correlation results exists somewhere.
  2. The second problem is that when i run corr<-lapply(PQ_data, cor_PQ), which has a line to save the outputs as tables (.txt) and images (.pdf) using part of the name of the original dataframe computed (e.g first element of PQ_data is "AgIX_E_PQ" so table and plot of cor_PQ(PQ_data[["AgIX_E_PQ"]] should get the names "mCorAgIX_E_PQ.txt" and "CorAgIX_E_PQ.pdf" respectively), im getting just one output (mCorX[[I]].txt and CorX[[i]].pdf) with the last dataframe correlation result. That is, tables and images for each dataframe correlation result are overwritten into this generics mCorX[[I]].txt, CorX[[i]].pdf files.

现在我想必须定义'i'或类似的东西来避免这种情况.我应该为PQ_data定义cor_PQ函数而不是X吗?

Now i guess have to define 'i' or something to avoid this. Should i define cor_PQ function for PQ_data instead X?

如果任何人都可以看到我失败的地方,请解决该问题,我将不胜感激.

If anyone can see where im failing, i will appreciate any help to solve this, please.

我的数据: PQ_data /将其保存在您的工作区中并使用它修复setwd. 我的代码:

My data: PQ_data /save it in your workspace and fix setwd with it. My code:

rm(list=ls(all=TRUE))
cat("\014")

setwd("C:/Users/Sol/Documents/ProyectoTítulo/CalidadAgua/Matrices/Regs") #my workspace

PQ_files<-list.files(path="C:/Users/Sol/Documents/ProyectoTítulo/CalidadAgua/Matrices/Regs",
                     pattern="\\_PQ.txt") #my list of 14 dataframes in my workspace.
PQ_data<-lapply(PQ_files, read.table) #read tables of the 14 dataframes in the list.
names(PQ_data)<-gsub("\\_PQ.txt","", PQ_files) #name the 14 dataframes with their original names.

#FUNCTION TO COMPUTE CORRELATIONS, SAVE TABLES AND PLOTS.
cor_PQ<-function(X) {
  corPQ<-cor(X, use="pairwise.complete.obs")
  outputname.txt<-paste0("mCor",deparse(substitute(X)),".txt")
  write.table(corPQ, file=outputname.txt)
  outputname.pdf<-paste0("Cor",deparse(substitute(X)),".pdf")
  pdf(outputname.pdf)
  plot(X)
  dev.off()
  return(corPQ)
}

corr<-lapply(PQ_data, cor_PQ)

此后,正如我所说,得到一个名为"corr"的列表,其中包含11个元素,这些元素包含来自列表(PQ_data)中每个数据帧的相关结果,但是当我将"corr"列表固定在其中时,我无法将它们作为表访问我的环境/数据窗口(它们不显示蓝色的R箭头以展开元素). ` 我只得到2个输出文件,分别称为mCorX [[I]].txt和CorX [[i]].pdf,它们仅显示最后一个数据帧相关结果,因为write.table和.pdf函数会覆盖10个先前计算的结果. 再次,我将不胜感激.我真的需要努力去抓住这个主意. 谢谢!!!

After this, as i said, a get a list called "corr" with 11 elements containing correlation results from each dataframe in my list (PQ_data), but i cant access them as tables when i pin the "corr" list in my environment/data window (they dont show the blue R arrow to expand the element). ` And i get only 2 output files called mCorX[[I]].txt and CorX[[i]].pdf showing only the last dataframe correlation result because the write.table and .pdf functions overwrite the results of the 10 previous calculations. Again, i will appreciate any help. I really need a push to catch the idea. Thanks!!!

推荐答案

lapply不会将列表名称发送给函数.因此,尽管该功能适用​​于单个文件,但不适用于文件列表.同样,由于文件没有名称,因此所有生成的文件都具有相同的名称,因此所有新文件都将覆盖先前存在的文件,最后只输出1个文件,这是列表中的最后一个元素.您可以使用以下函数,在该函数中,我们将名称作为不同的参数发送,以将名称分配给文件.

lapply doesn't send names of the list to the function. So although the function works for individual files it doesn't work with list of files. Also since there are no names to the files all the files generated are given the same name, hence all the new files overwrite the previously existing files and in the end you get output with only 1 file which is the last element in your list. You can use the below function where we send the names as different parameter to assign the name to the files.

cor_PQ<-function(X, Y) {
   corPQ<-cor(X, use="pairwise.complete.obs")
   outputname.txt<-paste0("mCor",Y,".txt")
   write.table(corPQ, file= outputname.txt)
   outputname.pdf<-paste0("Cor",Y,".pdf")
   pdf(outputname.pdf)
   plot(X)
   dev.off()
   return(corPQ)
}

现在使用Map来应用相同的功能.

Now use Map to apply the same function.

Map(cor_PQ, PQ_data, names(PQ_data))

我们还可以使用purrr中的imap来应用此功能.

We can also use imap from purrr to apply this function.

purrr::imap(PQ_data, cor_PQ)

这篇关于函数内的write.table应用于数据帧列表覆盖输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆