scanning data into r

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

scanning data into r

joel.green
Hey

I am having trouble importing data into R, my data field looks like this

21  TEST DATA
32  year:2012
33
34
 5
36

I require the the number at the start of each line however the text is not needed, i am struggling to get R to import the data with out changing the file itself?

how do i import the data, i have tried using comment.char=" ", however this didnt work, any help would be much appreciated thanks

Reply | Threaded
Open this post in threaded view
|

Re: scanning data into r

z2.0
Just read into a data.frame with read.table and then subset to use the first column.

e.g.,

your_desired_data <- data.frame(read.table("path_to_file", sep = " ", fill = T))
your_desired_data <- your_desired_data[,1]
Reply | Threaded
Open this post in threaded view
|

Re: scanning data into r

Justin Haynes
In reply to this post by joel.green
What have you tried?

What type of file are you trying to import from?

What do you want your data to look like in R?

take a look at ?read.table and ?readLines


On Wed, Mar 28, 2012 at 11:23 AM, joel.green <[hidden email]> wrote:

> Hey
>
> I am having trouble importing data into R, my data field looks like this
>
> 21  TEST DATA
> 32  year:2012
> 33
> 34
>  5
> 36
>
> I require the the number at the start of each line however the text is not
> needed, i am struggling to get R to import the data with out changing the
> file itself?
>
> how do i import the data, i have tried using comment.char=" ", however this
> didnt work, any help would be much appreciated thanks
>
>
>
> --
> View this message in context:
> http://r.789695.n4.nabble.com/scanning-data-into-r-tp4513182p4513182.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.
>

        [[alternative HTML version deleted]]

______________________________________________
[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: scanning data into r

David Winsemius
In reply to this post by joel.green

On Mar 28, 2012, at 2:23 PM, joel.green wrote:

> Hey
>
> I am having trouble importing data into R, my data field looks like  
> this
>
> 21  TEST DATA
> 32  year:2012
> 33
> 34
> 5
> 36
>

 > read.table(text="21  TEST DATA
+ 32  year:2012
+ 33
+ 34
+ 5
+ 36", fill=TRUE)[1]

   V1
1 21
2 32
3 33
4 34
5  5
6 36

> I require the the number at the start of each line however the text  
> is not
> needed, i am struggling to get R to import the data with out  
> changing the
> file itself?

Use , fill =TRUE)  and then throw away everything except the first  
column. Could also use readLines and split on " " and take first  
element with sapply( ...,  "[" , 1)
>
> how do i import the data, i have tried using comment.char=" ",  
> however this
> didnt work, any help would be much appreciated thanks
>
>
--

David Winsemius, MD
West Hartford, CT

______________________________________________
[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.