[R] How to import data from text file using scan() Function?
arun
smartpink111 at yahoo.com
Thu Oct 18 18:03:42 CEST 2012
Hi,
I agree with Sarah that read.table() will much easier in this case.
If you want scan(), then you can try this:
dat1<-scan(what=list("numeric","character","character","numeric"),text="
id name sex age
111 HELEN f 22
112 DONNA f 22
113 ERIC m 21
114 LINDA f 23
115 AXEL m 27
116 Madhuri f 32
117 Tarun m 39
118 Aashirya f 23
119 Nachik m 24
120 Leena f 32
",sep="",skip=2)
dat2<-do.call(data.frame,dat1)
colnames(dat2)<-c("id","name","sex","age")
dat2<-within(dat2,{id<-as.numeric(as.character(id));name<-as.character(name);sex<-as.character(sex);age<-as.numeric(as.character(age))})
str(dat2)
#'data.frame': 10 obs. of 4 variables:
# $ id : num 111 112 113 114 115 116 117 118 119 120
# $ name: chr "HELEN" "DONNA" "ERIC" "LINDA" ...
# $ sex : chr "f" "f" "m" "f" ...
# $ age : num 22 22 21 23 27 32 39 23 24 32
A.K.
----- Original Message -----
From: killerkarthick <karthick.gdi at gmail.com>
To: r-help at r-project.org
Cc:
Sent: Thursday, October 18, 2012 9:14 AM
Subject: [R] How to import data from text file using scan() Function?
Hi....
I have one text file which containing 4 variables with 10 observations.
I would like to import with scan() function. Please give some
suggestion............
Thanks...
Mydata set is.
id name sex age
111 HELEN f 22
112 DONNA f 22
113 ERIC m 21
114 LINDA f 23
115 AXEL m 27
116 Madhuri f 32
117 Tarun m 39
118 Aashirya f 23
119 Nachik m 24
120 Leena f 32
--
View this message in context: http://r.789695.n4.nabble.com/How-to-import-data-from-text-file-using-scan-Function-tp4646611.html
Sent from the R help mailing list archive at Nabble.com.
______________________________________________
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