[R] Reading data entered within an R program

Sam Thomas sam.thomas at revelanttech.com
Sun Jul 12 01:48:25 CEST 2009


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.




More information about the R-help mailing list