[R] Reading data entered within an R program

Muenchen, Robert A (Bob) muenchen at utk.edu
Sat Jul 11 22:38:23 CEST 2009


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




More information about the R-help mailing list