Gary Miller
2009-Dec-06 01:11 UTC
[R] SAS "datalines" or "cards" statement equivalent in R?
Hi R Users, Is there a equivalent command in R where I can read in raw data? For example I'm looking for equivalent R code for following SAS code: DATA survey; INPUT id sex $ age inc r1 r2 r3 ; DATALINES; 1 F 35 17 7 2 2 17 M 50 14 5 5 3 33 F 45 6 7 2 7 49 M 24 14 7 5 7 65 F 52 9 4 7 7 81 M 44 11 7 7 7 2 F 34 17 6 5 3 18 M 40 14 7 5 2 34 F 47 6 6 5 6 50 M 35 17 5 7 5 ; Any help would be highly appreciated, Gary [[alternative HTML version deleted]]
Jorge Ivan Velez
2009-Dec-06 01:24 UTC
[R] SAS "datalines" or "cards" statement equivalent in R?
Dear Gary, Here is a suggestion using read.table() and textConnection(): toread <- "id sex age inc r1 r2 r3 1 F 35 17 7 2 2 17 M 50 14 5 5 3 33 F 45 6 7 2 7 49 M 24 14 7 5 7 65 F 52 9 4 7 7 81 M 44 11 7 7 7 2 F 34 17 6 5 3 18 M 40 14 7 5 2 34 F 47 6 6 5 6 50 M 35 17 5 7 5" survey <- read.table(textConnection(toread), header = TRUE) closeAllConnections() survey See ?read.table and ?textConnection for more information. HTH, Jorge On Sat, Dec 5, 2009 at 8:11 PM, Gary Miller <> wrote:> Hi R Users, > > Is there a equivalent command in R where I can read in raw data? For > example > I'm looking for equivalent R code for following SAS code: > > DATA survey; > INPUT id sex $ age inc r1 r2 r3 ; > DATALINES; > 1 F 35 17 7 2 2 > 17 M 50 14 5 5 3 > 33 F 45 6 7 2 7 > 49 M 24 14 7 5 7 > 65 F 52 9 4 7 7 > 81 M 44 11 7 7 7 > 2 F 34 17 6 5 3 > 18 M 40 14 7 5 2 > 34 F 47 6 6 5 6 > 50 M 35 17 5 7 5 > ; > > Any help would be highly appreciated, > Gary > > [[alternative HTML version deleted]] > > ______________________________________________ > 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]]
Wensui Liu
2009-Dec-06 23:32 UTC
[R] SAS "datalines" or "cards" statement equivalent in R?
Gary,
if i were you, i would use scan().
here is a piece of code.
############################################
# DO DATA INPUT IN R CONSOLE WITH SCAN() #
#------------------------------------------#
# COMPARABLE SAS CODE: #
#------------------------------------------#
# data test; #
# input x1 x2 x3 y $ @@; #
# cards; #
# 71 . 3 0 158 14 3 0 #
# 128 5 4 1 #
# ; #
# run; #
############################################
test *<-* *data.frame*(scan(file = "",
what = *list*(x1 = 0, x2 = 0, x3 = 0, y =
"")))
71 NA 3 0 158 14 3 0
128 5 4 1
On Sat, Dec 5, 2009 at 8:11 PM, Gary Miller
<mail2garymiller@gmail.com>wrote:
> Hi R Users,
>
> Is there a equivalent command in R where I can read in raw data? For
> example
> I'm looking for equivalent R code for following SAS code:
>
> DATA survey;
> INPUT id sex $ age inc r1 r2 r3 ;
> DATALINES;
> 1 F 35 17 7 2 2
> 17 M 50 14 5 5 3
> 33 F 45 6 7 2 7
> 49 M 24 14 7 5 7
> 65 F 52 9 4 7 7
> 81 M 44 11 7 7 7
> 2 F 34 17 6 5 3
> 18 M 40 14 7 5 2
> 34 F 47 6 6 5 6
> 50 M 35 17 5 7 5
> ;
>
> Any help would be highly appreciated,
> Gary
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
>
--
=============================WenSui Liu
Blog : statcompute.spaces.live.com
Tough Times Never Last. But Tough People Do. - Robert Schuller
=============================
[[alternative HTML version deleted]]
Marshall Feldman
2009-Dec-07 15:53 UTC
[R] SAS "datalines" or "cards" statement equivalent in R?
Regarding the various methods people have suggested, what if a typical
tab-delimited data line looks like:
SMS11000000000000001 1990 M01 688.0
and the SAS INPUT statement is
INPUT survey $ 1-2 seasonal $ 3 state $ 4-5 area $ 6-10 supersector
$ 11-12 @13 industry $8. datatype $ 21-22 year period $ value footnote $ ;
Note that most data lines have no footnote item, as in the sample.
Here (I think) we'd want all the character variables to be read as
factors, possibly "year" as a date, and "value" as numeric.
Marsh
On Sat, Dec 5, 2009 at 8:11 PM, Gary Miller <>
wrote:>
> >> Hi R Users,
> >>
> >> Is there a equivalent command in R where I can read in raw data?
For
> >> example
> >> I'm looking for equivalent R code for following SAS code:
> >>
> >> DATA survey;
> >> INPUT id sex $ age inc r1 r2 r3 ;
> >> DATALINES;
> >> 1 F 35 17 7 2 2
> >> 17 M 50 14 5 5 3
> >> 33 F 45 6 7 2 7
> >> 49 M 24 14 7 5 7
> >> 65 F 52 9 4 7 7
> >> 81 M 44 11 7 7 7
> >> 2 F 34 17 6 5 3
> >> 18 M 40 14 7 5 2
> >> 34 F 47 6 6 5 6
> >> 50 M 35 17 5 7 5
> >> ;
> >>
> >> Any help would be highly appreciated,
> >> Gary