How to import data from text file using scan() Function?

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

How to import data from text file using scan() Function?

killerkarthick
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
Reply | Threaded
Open this post in threaded view
|

Re: How to import data from text file using scan() Function?

Sarah Goslee
Why do you need to use scan()? read.table() would be much easier.

Sarah

On Thu, Oct 18, 2012 at 9:14 AM, killerkarthick <[hidden email]> wrote:

> 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
>
>

--
Sarah Goslee
http://www.functionaldiversity.org

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: How to import data from text file using scan() Function?

arun kirshna
In reply to this post by killerkarthick
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 <[hidden email]>
To: [hidden email]
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.

______________________________________________
[hidden email] 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.


______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: How to import data from text file using scan() Function?

arun kirshna
This post has NOT been accepted by the mailing list yet.
In reply to this post by killerkarthick
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.