franklin johnson
2013-Oct-05 01:21 UTC
[R] make a file from four individual files. but keys can be missing among files.
Hello, I have a list of four files. Each file is a list of gene models, and each gene model has attributes in file columns. The sample_week-over-sample_week fold change value foreach i in file is in column 5. The gene model ID is in column 1. To make it more complicated, each gene i may not be found in all four files. I've written R scripts to try to 1) write a function to iterate over each file in the working directory, 2) if a gene model, i, is missing in a file(x), paste an "NA" as the value for that key and 3) make one master file from the four files with column names as indicated below in the code. function(g)( for( i in x){ if( i == NULL) print( text("NA")) else( print(x[i,5])) #Fold-change value )) files <- list.files( path=getwd(), pattern="*.txt", full.names=T, recursive=FALSE) lapply( files, function(x) { x <- read.table( x, header=T) # load file # apply function out <- function(g) # write to file write.table( out, "myfile.txt", sep="\t", quote=F, row.names=F, col.names=T) colnames(out)[1] = "ca02" colnames(out)[2] = "ca24" colnames(out)[3] = "ca48" colnames(out)[4] = "ca812" }) }) The code has no errors, but the file doesn't write to my working directory. I don't think this path is the problem either. I've seen the do.call function, thinking this may be the way to go. Also, if I'd like to add a third column to the master file from the four files, for example, the BLAST description for that gene model, i, in column 7, how to handle this command as well? I'm kinda new to R but hit a wall when it comes to this level, as I have had no classroom training on writing code. Hopefully, I can break on through with your assistance. Thanks much and have a nice day. -- Franklin [[alternative HTML version deleted]]
Sarah Goslee
2013-Oct-05 15:20 UTC
[R] make a file from four individual files. but keys can be missing among files.
Hi, You didn't provide a reproducible example, which makes helping a whole lot harder. You're misunderstanding how to define a new function. Not only is g specified incorrectly, you're creating a new function called out within your loop and trying to save that to disk. You need to do more like this g <- function(args) { Does stuff Return value } And then call it with out <- g(args) But it seems to me that merge() might do the trick. Sarah On Friday, October 4, 2013, franklin johnson wrote:> Hello, > > I have a list of four files. Each file is a list of gene models, and each > gene model has attributes in file columns. The sample_week-over-sample_week > fold change value foreach i in file is in column 5. The gene model ID is in > column 1. To make it more complicated, each gene i may not be found in all > four files. I've written R scripts to try to 1) write a function to iterate > over each file in the working directory, 2) if a gene model, i, is missing > in a file(x), paste an "NA" as the value for that key and 3) make one > master file from the four files with column names as indicated below in the > code. > > function(g)( > > for( i in x){ > if( i == NULL) > print( text("NA")) > else( print(x[i,5])) #Fold-change value > )) > > files <- list.files( path=getwd(), pattern="*.txt", full.names=T, > recursive=FALSE) > lapply( files, function(x) { > x <- read.table( x, header=T) # load file > # apply function > out <- function(g) > # write to file > write.table( out, "myfile.txt", sep="\t", quote=F, row.names=F, > col.names=T) > colnames(out)[1] = "ca02" > colnames(out)[2] = "ca24" > colnames(out)[3] = "ca48" > colnames(out)[4] = "ca812" > }) > }) > > The code has no errors, but the file doesn't write to my working directory. > I don't think this path is the problem either. > > I've seen the do.call function, thinking this may be the way to go. > > Also, if I'd like to add a third column to the master file from the four > files, for example, the BLAST description for that gene model, i, in column > 7, how to handle this command as well? I'm kinda new to R but hit a wall > when it comes to this level, as I have had no classroom training on writing > code. Hopefully, I can break on through with your assistance. Thanks much > and have a nice day. > -- > Franklin > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org <javascript:;> 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. >-- Sarah Goslee http://www.stringpage.com http://www.sarahgoslee.com http://www.functionaldiversity.org [[alternative HTML version deleted]]