Muenchen, Robert A (Bob)
2009-Jul-11 20:38 UTC
[R] Reading data entered within an R program
Dear R-helpers, I know of two ways to reading data within an R program, using textConnection and stdin (demo program below). I've Googled about and looked in several books for comparisons of the two approaches but haven't found anything. Are there any particular advantages or disadvantages to these two approaches? If you were teaching R beginners, which would you present? Thanks, Bob http://RforSASandSPSSusers.com # R Program to Read Data Within a Program. # Very similar to SAS datalines or cards statements, # and SPSS BEGIN DATA / END DATA commands. # This stores the data as one long text string. mystring <- "workshop,gender,q1,q2,q3,q4 01,1,f,1,1,5,1 02,2,f,2,1,4,1 03,1,f,2,2,4,3 04,2, ,3,1, ,3 05,1,m,4,5,2,4 06,2,m,5,4,5,5 07,1,m,5,3,4,4 08,2,m,4,5,5,5" # The textConnection function allows read.csv to # read data from the text string just as it would # from a file. # The leading zero on first column helps show that # R is storing row names as a character vector. mydata <- read.csv( textConnection(mystring) ) mydata mydata <- read.csv( stdin() ) workshop,gender,q1,q2,q3,q4 01,1,f,1,1,5,1 02,2,f,2,1,4,1 03,1,f,2,2,4,3 04,2, ,3,1, ,3 05,1,m,4,5,2,4 06,2,m,5,4,5,5 07,1,m,5,3,4,4 08,2,m,4,5,5,5 #The blank line above tells R to stop reading. mydata # Read it again stripping out blanks and setting # "nothing" to be missing for gender. mydata <- read.csv( stdin(), strip.white=TRUE, na.strings="" ) workshop,gender,q1,q2,q3,q4 01,1,f,1,1,5,1 02,2,f,2,1,4,1 03,1,f,2,2,4,3 04,2, ,3,1, ,3 05,1,m,4,5,2,4 06,2,m,5,4,5,5 07,1,m,5,3,4,4 08,2,m,4,5,5,5 mydata
*hum* I prefer stdin() because you not need to explain to your student or beginners about TextConnection. cheers milton On Sat, Jul 11, 2009 at 4:38 PM, Muenchen, Robert A (Bob) <muenchen@utk.edu>wrote:> Dear R-helpers, > > I know of two ways to reading data within an R program, using > textConnection and stdin (demo program below). I've Googled about and > looked in several books for comparisons of the two approaches but > haven't found anything. Are there any particular advantages or > disadvantages to these two approaches? If you were teaching R beginners, > which would you present? > > Thanks, > Bob > http://RforSASandSPSSusers.com <http://rforsasandspssusers.com/> > > > # R Program to Read Data Within a Program. > # Very similar to SAS datalines or cards statements, > # and SPSS BEGIN DATA / END DATA commands. > > # This stores the data as one long text string. > > mystring <- > "workshop,gender,q1,q2,q3,q4 > 01,1,f,1,1,5,1 > 02,2,f,2,1,4,1 > 03,1,f,2,2,4,3 > 04,2, ,3,1, ,3 > 05,1,m,4,5,2,4 > 06,2,m,5,4,5,5 > 07,1,m,5,3,4,4 > 08,2,m,4,5,5,5" > > # The textConnection function allows read.csv to > # read data from the text string just as it would > # from a file. > # The leading zero on first column helps show that > # R is storing row names as a character vector. > > mydata <- read.csv( textConnection(mystring) ) > mydata > > > mydata <- read.csv( stdin() ) > workshop,gender,q1,q2,q3,q4 > 01,1,f,1,1,5,1 > 02,2,f,2,1,4,1 > 03,1,f,2,2,4,3 > 04,2, ,3,1, ,3 > 05,1,m,4,5,2,4 > 06,2,m,5,4,5,5 > 07,1,m,5,3,4,4 > 08,2,m,4,5,5,5 > > #The blank line above tells R to stop reading. > mydata > > # Read it again stripping out blanks and setting > # "nothing" to be missing for gender. > > mydata <- read.csv( stdin(), strip.white=TRUE, na.strings="" ) > workshop,gender,q1,q2,q3,q4 > 01,1,f,1,1,5,1 > 02,2,f,2,1,4,1 > 03,1,f,2,2,4,3 > 04,2, ,3,1, ,3 > 05,1,m,4,5,2,4 > 06,2,m,5,4,5,5 > 07,1,m,5,3,4,4 > 08,2,m,4,5,5,5 > > mydata > > ______________________________________________ > 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<http://www.r-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
Both will work if you copy and paste directly into an R session but textConnection has the advantage that you can place it in a file and source it and it still works. On Sat, Jul 11, 2009 at 4:38 PM, Muenchen, Robert A (Bob) <muenchen@utk.edu>wrote:> Dear R-helpers, > > I know of two ways to reading data within an R program, using > textConnection and stdin (demo program below). I've Googled about and > looked in several books for comparisons of the two approaches but > haven't found anything. Are there any particular advantages or > disadvantages to these two approaches? If you were teaching R beginners, > which would you present? > > Thanks, > Bob > http://RforSASandSPSSusers.com > > > # R Program to Read Data Within a Program. > # Very similar to SAS datalines or cards statements, > # and SPSS BEGIN DATA / END DATA commands. > > # This stores the data as one long text string. > > mystring <- > "workshop,gender,q1,q2,q3,q4 > 01,1,f,1,1,5,1 > 02,2,f,2,1,4,1 > 03,1,f,2,2,4,3 > 04,2, ,3,1, ,3 > 05,1,m,4,5,2,4 > 06,2,m,5,4,5,5 > 07,1,m,5,3,4,4 > 08,2,m,4,5,5,5" > > # The textConnection function allows read.csv to > # read data from the text string just as it would > # from a file. > # The leading zero on first column helps show that > # R is storing row names as a character vector. > > mydata <- read.csv( textConnection(mystring) ) > mydata > > > mydata <- read.csv( stdin() ) > workshop,gender,q1,q2,q3,q4 > 01,1,f,1,1,5,1 > 02,2,f,2,1,4,1 > 03,1,f,2,2,4,3 > 04,2, ,3,1, ,3 > 05,1,m,4,5,2,4 > 06,2,m,5,4,5,5 > 07,1,m,5,3,4,4 > 08,2,m,4,5,5,5 > > #The blank line above tells R to stop reading. > mydata > > # Read it again stripping out blanks and setting > # "nothing" to be missing for gender. > > mydata <- read.csv( stdin(), strip.white=TRUE, na.strings="" ) > workshop,gender,q1,q2,q3,q4 > 01,1,f,1,1,5,1 > 02,2,f,2,1,4,1 > 03,1,f,2,2,4,3 > 04,2, ,3,1, ,3 > 05,1,m,4,5,2,4 > 06,2,m,5,4,5,5 > 07,1,m,5,3,4,4 > 08,2,m,4,5,5,5 > > mydata > > ______________________________________________ > 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. >[[alternative HTML version deleted]]
threeftmetered Sent from my Windows Mobile? phone. -----Original Message----- From: Muenchen, Robert A (Bob) <muenchen at utk.edu> Sent: Saturday, July 11, 2009 4:43 PM To: R-help at r-project.org <R-help at r-project.org> Subject: [R] Reading data entered within an R program Dear R-helpers, I know of two ways to reading data within an R program, using textConnection and stdin (demo program below). I've Googled about and looked in several books for comparisons of the two approaches but haven't found anything. Are there any particular advantages or disadvantages to these two approaches? If you were teaching R beginners, which would you present? Thanks, Bob http://RforSASandSPSSusers.com # R Program to Read Data Within a Program. # Very similar to SAS datalines or cards statements, # and SPSS BEGIN DATA / END DATA commands. # This stores the data as one long text string. mystring <- "workshop,gender,q1,q2,q3,q4 01,1,f,1,1,5,1 02,2,f,2,1,4,1 03,1,f,2,2,4,3 04,2, ,3,1, ,3 05,1,m,4,5,2,4 06,2,m,5,4,5,5 07,1,m,5,3,4,4 08,2,m,4,5,5,5" # The textConnection function allows read.csv to # read data from the text string just as it would # from a file. # The leading zero on first column helps show that # R is storing row names as a character vector. mydata <- read.csv( textConnection(mystring) ) mydata mydata <- read.csv( stdin() ) workshop,gender,q1,q2,q3,q4 01,1,f,1,1,5,1 02,2,f,2,1,4,1 03,1,f,2,2,4,3 04,2, ,3,1, ,3 05,1,m,4,5,2,4 06,2,m,5,4,5,5 07,1,m,5,3,4,4 08,2,m,4,5,5,5 #The blank line above tells R to stop reading. mydata # Read it again stripping out blanks and setting # "nothing" to be missing for gender. mydata <- read.csv( stdin(), strip.white=TRUE, na.strings="" ) workshop,gender,q1,q2,q3,q4 01,1,f,1,1,5,1 02,2,f,2,1,4,1 03,1,f,2,2,4,3 04,2, ,3,1, ,3 05,1,m,4,5,2,4 06,2,m,5,4,5,5 07,1,m,5,3,4,4 08,2,m,4,5,5,5 mydata ______________________________________________ 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.