Hello :) I'm just starting out with R and would appreciate your help with a couple of problems I am running into. I have used Sys.glob to get a list of all filenames having a particular file extension (in my case, *.txt) I would now like to use this list in the following manner: I would like to use each filename from the list and open that file into a tab separated matrix and proceed. How can I go about iterating through each filename in the list and then opening each of the files? I believe as.matrix can be used to open the txt file as a table, is that correct? I know these are beginner queries but I would love your help. Thank you in advance :) [[alternative HTML version deleted]]
Have you read "An Introduction to R" (ships with R) or other of the many web tutorials. If not, please do so before posting. -- Bert On Thu, Feb 28, 2013 at 4:30 AM, Sahana Srinivasan <sahanasrinivasan.91 at gmail.com> wrote:> Hello :) > I'm just starting out with R and would appreciate your help with a couple > of problems I am running into. > I have used Sys.glob to get a list of all filenames having a particular > file extension (in my case, *.txt) > I would now like to use this list in the following manner: I would like to > use each filename from the list and open that file into a tab separated > matrix and proceed. > How can I go about iterating through each filename in the list and then > opening each of the files? > I believe as.matrix can be used to open the txt file as a table, is that > correct? > > I know these are beginner queries but I would love your help. Thank you in > advance :) > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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.-- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm
1. I have forwarded this to the list in order to increase your chance of getting a response. I hope that is OK. 2. I would have thought that by putting in the effort to understand the tutorial is how a complete beginner advances beyond that stage. Cheers, Bert On Thu, Feb 28, 2013 at 1:43 PM, Sahana Srinivasan <sahanasrinivasan.91 at gmail.com> wrote:> Um. Yes. I wouldn't have posted without having looked stuff up. Most > documentation is written in a way that is a little tough for a complete > beginner to understand. > > > > On Thu, Feb 28, 2013 at 9:41 PM, Bert Gunter <gunter.berton at gene.com> wrote: >> >> Have you read "An Introduction to R" (ships with R) or other of the >> many web tutorials. If not, please do so before posting. >> >> -- Bert >> >> On Thu, Feb 28, 2013 at 4:30 AM, Sahana Srinivasan >> <sahanasrinivasan.91 at gmail.com> wrote: >> > Hello :) >> > I'm just starting out with R and would appreciate your help with a >> > couple >> > of problems I am running into. >> > I have used Sys.glob to get a list of all filenames having a particular >> > file extension (in my case, *.txt) >> > I would now like to use this list in the following manner: I would like >> > to >> > use each filename from the list and open that file into a tab separated >> > matrix and proceed. >> > How can I go about iterating through each filename in the list and then >> > opening each of the files? >> > I believe as.matrix can be used to open the txt file as a table, is that >> > correct? >> > >> > I know these are beginner queries but I would love your help. Thank you >> > in >> > advance :) >> > >> > [[alternative HTML version deleted]] >> > >> > ______________________________________________ >> > R-help at 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. >> >> >> >> -- >> >> Bert Gunter >> Genentech Nonclinical Biostatistics >> >> Internal Contact Info: >> Phone: 467-7374 >> Website: >> >> http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm > >-- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm
On Feb 28, 2013, at 4:30 AM, Sahana Srinivasan wrote:> Hello :) > I'm just starting out with R and would appreciate your help with a couple > of problems I am running into.You do need to work through the examples in "Introduction to R"> I have used Sys.glob to get a list of all filenames having a particular > file extension (in my case, *.txt)When posting to rhelp you should offer code as well as a description.> I would now like to use this list in the following manner: I would like to > use each filename from the list and open that file into a tab separated > matrix and proceed.?read.delim "Matrix" is sometimes used as a generic English word describing a regular arrangement of data. When discussing the R language, you should use it only when talking about a specific sort of data-object which will return TRUE for: is.matrix(object). And then depending on whether you actually want a matrix or will be satisfied by the data-frame object that read.delim would have returned, you may or may not need: ?data.matrix> How can I go about iterating through each filename in the list and then > opening each of the files?lapply( fil_list, read.delim)> I believe as.matrix can be used to open the txt file as a table, is that > correct?No. Please read the help page for `as.matrix` more carefully. It does not take a file or connection argument.> > I know these are beginner queries but I would love your help. Thank you in > advance :)-- David Winsemius Alameda, CA, USA
I suppose you have your filenames stored in a character vector, which I will name "myfiles". (Within R, it is not a "list"; lists have a special structure). There is no such thing as a "tab separated matrix" in R. "tab separated" would refer to the file, I presume. for (nm in myfiles) { tmpdat <- read.table(nm, ## put appropriate arguments here ) tmpmat <- as.matrix(tmpdat) ## might or might give you what you want, ## depending on what is in the files ## it might make sense to use scan() instead, depending on how ## your text files are structured. ## do whatever with tmpdat or tmpmat } When the loop is done, however, tmpdat and tmpmat will have only the values from the last file. Saving all of them for later use is a different question. Often, you will get better help from r-help if you show what you have tried, by which I mean showing some R code. As much as possible, using examples that other people can run for themselves. (See the posting guide) Hope this helps -Don -- Don MacQueen Lawrence Livermore National Laboratory 7000 East Ave., L-627 Livermore, CA 94550 925-423-1062 On 2/28/13 4:30 AM, "Sahana Srinivasan" <sahanasrinivasan.91 at gmail.com> wrote: Hello :) I'm just starting out with R and would appreciate your help with a couple of problems I am running into. I have used Sys.glob to get a list of all filenames having a particular file extension (in my case, *.txt) I would now like to use this list in the following manner: I would like to use each filename from the list and open that file into a tab separated matrix and proceed. How can I go about iterating through each filename in the list and then opening each of the files? I believe as.matrix can be used to open the txt file as a table, is that correct? I know these are beginner queries but I would love your help. Thank you in advance :) [[alternative HTML version deleted]] ______________________________________________ R-help at 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.