Hi, directory<- "/home/arunksa111/data.new" #first function filelist<-function(directory,number,list1){ setwd(directory) filelist1<-dir(directory) direct<-dir(directory,pattern = paste("MSMS_",number,"PepInfo.txt",sep=""), full.names = FALSE, recursive = TRUE) list1<-lapply(direct, function(x) read.table(x,header=TRUE, sep = "\t",stringsAsFactors=FALSE)) names(list1)<-filelist1 list2<- list(filelist1,list1) return(list2) } foldernames1<-filelist(directory,23,list1)[[1]] foldernames1 #[1] "a1" "c1" "c2" "c3" "t1" "t2" lista<-filelist(directory,23,list1)[[2]] #lista output
FacGroup<- c("c1","c3","t2") #Second function f<-function(listRes,Toselect){ res2<-split(listRes,gsub("[0-9]","",names(listRes))) res3<-lapply(seq_along(res2),function(i) lapply(res2[[i]],function(x) x[x[["FDR"]]<0.01,c("Seq","Mod","z","spec")])) res4<-lapply(res3,function(x) x[names(x)[names(x)%in%Toselect]]) res4New<- lapply(res4,function(x) lapply(names(x), function(i) do.call(rbind,lapply(x[i],function(x) cbind(folder_name=i,x))) )) library(plyr) library(data.table) res5<-lapply(res4New,function(x) lapply(x,function(x1){ x1<- data.table(x1);x1[,spec:=paste(spec,collapse=","),by=c("Seq","Mod","z")]})) res6<- lapply(res5,function(x) lapply(x,function(x1) {x1$counts<-sapply(x1$spec, function(x2) length(gsub("\\s", "", unlist(strsplit(x2, ",")))));x3<-as.data.frame(x1);names(x3)[6]<- as.character(unique(x3$folder_name));x3[,-c(1,5)]})) res7<-lapply(res6,function(x) Reduce(function(...) merge(...,by=c("Seq","Mod","z"),all=TRUE),x)) res8<-res7[lapply(res7,length)!=0] res9<- Reduce(function(...) merge(...,by=c("Seq","Mod","z"),all=TRUE),res8) res9[is.na(res9)] <- 0 return(res9) } f(lista,FacGroup) head(f(lista,FacGroup)) # Seq Mod z c1 c3 t2 #1 aAAAAAAAAAAAAAATATAGPR 1-n_acPro/ 2 0 0 1 #2 aAAAAAAAAAAASSPVGVGQR 1-n_acPro/ 2 0 0 1 #3 aAAAAAAAAAGAAGGR 1-n_acPro/ 2 0 0 1 #4 aAAAAAAAGAAGGRGSGPGRR 1-n_acPro/ 2 1 0 0 #5 AAAAAAALQAK 2 0 1 1 #6 aAAAAAGAGPEMVR 1-n_acPro/ 2 0 0 2 resCounts<- f(lista,FacGroup) t.test.p.value <- function(...) { obj<-try(t.test(...), silent=TRUE) if (is(obj, "try-error")) return(NA) else return(obj$p.value) } #3rd function for p-value fpv<- function(Countdata){ resNew<-do.call(cbind,lapply(split(names(Countdata)[4:ncol(Countdata)],gsub("[0-9]","",names(Countdata)[4:ncol(Countdata)])), function(i) {x<-if(ncol(Countdata[i])>1) rowSums(Countdata[i]) else Countdata[i]; colnames(x)<-NULL;x})) indx<-combn(names(resNew),2) resPval<-do.call(cbind,lapply(seq_len(ncol(indx)),function(i) {x<-as.data.frame(apply(resNew[,indx[,i]],1,t.test.p.value)); colnames(x)<-paste("Pvalue",paste(indx[,i],collapse=""),sep="_");x})) resF<-cbind(resCounts,resPval) resF } fpv(resCounts) A.K. ________________________________ From: Vera Costa <veracosta...@gmail.com> To: arun <smartpink...@yahoo.com> Sent: Thursday, February 28, 2013 11:30 AM Subject: new question Sorry about my question, but I need a new small thing...I need to split my function to read data and to do the treatment of the data. At first I need to know the "names" of the files and read data, and after a new function with my analysis. So, I did this directory<-"C:/Users/Vera Costa/Desktop/data.new" filelist<-function(directory,number){ setwd(directory) filelist<-dir(directory) return(filelist) direct<-dir(directory,pattern = paste("MSMS_",number,"PepInfo.txt",sep=""), full.names = FALSE, recursive = TRUE) lista<-lapply(direct, function(x) read.table(x,header=TRUE, sep = "\t")) names(lista)<-filelist return(lista) } filelist(directory,23) ###"a1" "a2" "c1" "c2" "c3" "t1" "t2" and after f<-function(filelist,FacGroup){ res2<-split(lista,names(lista)) res3<- lapply(res2,function(x) {names(x)<-paste(gsub(".*_","",names(x)),1:length(x),sep="");x}) res3 #Freq FDR<0.01 res4<-lapply(seq_along(res3),function(i) lapply(res3[[i]],function(x) x[x[["FDR"]]<0.01,c("Seq","Mod","z","spec")])) names(res4)<- names(res2) res4 res4New<-lapply(res4,function(x) lapply(names(x),function(i) do.call(rbind,lapply(x[i],function(x) cbind(folder_name=i,x))) )) res5<- lapply(res4New,function(x) if(length(x)>1) tail(x,-1) else NULL) library(plyr) library(data.table) res6<- lapply(res5,function(x) lapply(x,function(x1) {x1<-data.table(x1); x1[,spec:=past How can I "ask lista in second function? Could you help me? ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.