Need HELP: how find a csv file?

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

Need HELP: how find a csv file?

F86
This post was updated on .
Hey,

I am having some problems with importing a csv file into R and then saving it for analyzing.

I got a csv file ( skater.csv) which i could read by typing:    read.csv(file="/Users/kama/Desktop/skatter.csv", header=TRUE, sep=";")

However, when i enter:    skatter.csv<-read.csv("skatter.csv", header=TRUE) i get this message:
Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") :
I have tried with:   skatter.csv<-file.choose() and other codes to find the file but it does not work.
Please help me fix this problem, i have been sitting with this one in 4 hours..



What i need is to import this file and analyze it using for example histogram.

I have Mac(update) and the file is saved in csv file... and I'm quite new user of R.



Thank you very much!


Reply | Threaded
Open this post in threaded view
|

Re: HELP me please with import of csv to R

Sarah Goslee
Hi,

On Tue, Jul 10, 2012 at 12:48 PM, F86 <[hidden email]> wrote:
> Hey,
>
> I am having problems with importing a csv file to R.
>
> I could read the file by typing:
> read.csv(file="/Users/kama/Desktop/skatter.csv", header=TRUE, sep=";")

So that command does work?


> However, i can not analyze the "skatter" - for ex, when i type:     skatter
> = read.csv("skatter.csv")

Then you need the above command, not what you type here:

skatter <- read.csv(file="/Users/kama/Desktop/skatter.csv",
header=TRUE, sep=";")

But note that if sep=";" then you don't have a csv file and should
properly use read.table() instead.

> i get this message:
>
> Error in file(file, "rt") : cannot open the connection
> In addition: Warning message:
> In file(file, "rt") :
>
> What i need is to import this file and analyze it using for example
> histogram.
>
> I have Mac(update) and the file is saved in csv file... and I'm quite new
> user of R.

That error means that R can't find the file where you told it to look.
Specifying the full and complete path as in your first example should
work.

If you're having problems with paths (which are a Mac issue and not at
all an R issue), you could also try

read.table(file.choose(), header=TRUE, sep=";")

I think that file.choose() should work on Mac.

The Intro to R document that came with R might also be of use.

Sarah

--
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: HELP me please with import of csv to R

FJ M
In reply to this post by F86

Try putting the data into some kind of object. I'm not sure what R does with the data from read.csv. I always
 
1) read the data into an object
2) print the data out
3) attach the data so that the headers become objects that contain the data
4) and yes, print the data out using ls
5) check the output file just in case one of 1-4 throw an error.
 
 
v_data <- read.table("C:\\Users\\Frank\\Options\\CBOE\\VZ\\2012\\VZ_2012_03_Summary_V0_R.TXT",header=T)
v_data
attach(v_data)
ls(v_data)
 

GL
 
Frank
Chicago, IL
 
 

> Date: Tue, 10 Jul 2012 09:48:04 -0700
> From: [hidden email]
> To: [hidden email]
> Subject: [R] HELP me please with import of csv to R
>
> Hey,
>
> I am having problems with importing a csv file to R.
>
> I could read the file by typing:
> read.csv(file="/Users/kama/Desktop/skatter.csv", header=TRUE, sep=";")
>
> However, i can not analyze the "skatter" - for ex, when i type: skatter
> = read.csv("skatter.csv")
>
> i get this message:
>
> Error in file(file, "rt") : cannot open the connection
> In addition: Warning message:
> In file(file, "rt") :
>
> What i need is to import this file and analyze it using for example
> histogram.
>
> I have Mac(update) and the file is saved in csv file... and I'm quite new
> user of R.
>
>
>
> Thank you very much!
>
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/HELP-me-please-with-import-of-csv-to-R-tp4636019.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: HELP me please with import of csv to R

Peter Dalgaard-2
In reply to this post by Sarah Goslee

On Jul 10, 2012, at 21:44 , Sarah Goslee wrote:
>
> But note that if sep=";" then you don't have a csv file and should
> properly use read.table() instead.

That's not actually true. In a substantial part of the world, csv files are semicolon separated. That's what read.csv2() is for. (Yes, it is silly, please don't get me started...)

--
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: [hidden email]  Priv: [hidden email]

______________________________________________
[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: HELP me please with import of csv to R

Sarah Goslee
That is silly, but I have learned something. Thanks.

Though honestly, I've never seen the advantage of read.csv() over the more
versatile read.table().

Sarah

On Tuesday, July 10, 2012, peter dalgaard wrote:

>
> On Jul 10, 2012, at 21:44 , Sarah Goslee wrote:
> >
> > But note that if sep=";" then you don't have a csv file and should
> > properly use read.table() instead.
>
> That's not actually true. In a substantial part of the world, csv files
> are semicolon separated. That's what read.csv2() is for. (Yes, it is silly,
> please don't get me started...)
>
> --
> Peter Dalgaard, Professor,
> Center for Statistics, Copenhagen Business School
> Solbjerg Plads 3, 2000 Frederiksberg, Denmark
> Phone: (+45)38153501
> Email: [hidden email] <javascript:;>  Priv: [hidden email] <javascript:;>
>
>
>
>
>
>
>
>
>

--
Sarah Goslee
http://www.stringpage.com
http://www.sarahgoslee.com
http://www.functionaldiversity.org

        [[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: HELP me please with import of csv to R

Peter Dalgaard-2

On Jul 11, 2012, at 01:24 , Sarah Goslee wrote:

> That is silly, but I have learned something. Thanks.
>
(The silliest bit was when someone decided that numeric data files should use locale-dependent conventions, notably decimal separators...)

> Though honestly, I've never seen the advantage of read.csv() over the more versatile read.table().

It's mainly that other software likes to write such files, so it is convenient with a simple function to read them. Same thing with read.delim(). There's also a point in trying to get all settings right, especially quotes and comments can throw users off.

>
> Sarah
>
> On Tuesday, July 10, 2012, peter dalgaard wrote:
>
> On Jul 10, 2012, at 21:44 , Sarah Goslee wrote:
> >
> > But note that if sep=";" then you don't have a csv file and should
> > properly use read.table() instead.
>
> That's not actually true. In a substantial part of the world, csv files are semicolon separated. That's what read.csv2() is for. (Yes, it is silly, please don't get me started...)
>
> --
> Peter Dalgaard, Professor,
> Center for Statistics, Copenhagen Business School
> Solbjerg Plads 3, 2000 Frederiksberg, Denmark
> Phone: (+45)38153501
> Email: [hidden email]  Priv: [hidden email]
>
>
>
>
>
>
>
>
>
>
> --
> Sarah Goslee
> http://www.stringpage.com
> http://www.sarahgoslee.com
> http://www.functionaldiversity.org

--
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: [hidden email]  Priv: [hidden email]

______________________________________________
[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: HELP me please with import of csv to R

Michael Weylandt
In reply to this post by FJ M
On Tue, Jul 10, 2012 at 4:23 PM, FJ M <[hidden email]> wrote:

> 3) attach the data so that the headers become objects that contain the data
> attach(v_data)

This is a discouraged practice as it leads to difficult to trace
errors and non-local effects. Some "big names" of the R universe
suggest it [I think V of V&R mentioned it in Nashville] but others are
just as strongly against, particularly for beginners.

Beter is to make use of formula= and data= arguments when available,
and with/within elsewise.

Best,
Michael

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

Re: HELP me please with import of csv to R

F86
In reply to this post by FJ M
Thank you! It is fixed now.

However, now when I'm trying with hist(skatter)   i get this message:  Error in hist.default(skatter) : 'x' must be numeric

I don't know what I'm doing wrong but it worked perfectly on windows some weeks ago.

My data skater looks like this:

> skatter
           Kommuner   Skatt
1          Botkyrka 32,23
2          Danderyd 29,45
3             Ekero 31,33
4           Haninge 31,88
5          Huddinge 32,05
6          Jarfalla 30,73
7           Lidingo 30,72
8             Nacka 30,71
9         Norrtalje 32,22
10          Nykvarn 32,05
11        Nynashamn 31,93
12            Salem    32
13          Sigtuna 32,08
14       Sollentuna  30,3
-
-
-
..until 290


Everything should be right since its a csv, the number of municipalities (Swedish: Kommuner) is 290 and all objects has a tax rate ( Skatt)
F86
Reply | Threaded
Open this post in threaded view
|

Re: HELP me please with import of csv to R

F86
In reply to this post by Peter Dalgaard-2
Thank you! It is fixed now.

However, now when I'm trying with hist(skatter)   i get this message:  Error in hist.default(skatter) : 'x' must be numeric

I don't know what I'm doing wrong but it worked perfectly on windows some weeks ago.

My data skatter looks like this:

> skatter
           Kommuner   Skatt
1          Botkyrka 32,23
2          Danderyd 29,45
3             Ekero 31,33
4           Haninge 31,88
5          Huddinge 32,05
6          Jarfalla 30,73
7           Lidingo 30,72
8             Nacka 30,71
9         Norrtalje 32,22
10          Nykvarn 32,05
11        Nynashamn 31,93
12            Salem    32
13          Sigtuna 32,08
14       Sollentuna  30,3
-
-
-
..until 290


Everything should be right since its a csv, the number of municipalities (Swedish: Kommuner) is 290 and all objects has a tax rate ( Skatt)

Anyone?
Reply | Threaded
Open this post in threaded view
|

Re: HELP me please with import of csv to R

William Dunlap
In reply to this post by F86
Did you use read.table(filename, ..., dec=",") when importing
the data (so "30,3" is read as the number 30 and 3 tenths instead
of as the character string "30,3")?

Whenever importing data follow up by using str() or summary()
on its output, before doing any further analysis.  E.g.,
  > bad <- read.table(header=TRUE, text="x y\n1,1 2,1\n1 2\n")
  > str(bad)
  'data.frame':   2 obs. of  2 variables:
   $ x: Factor w/ 2 levels "1","1,1": 2 1
   $ y: Factor w/ 2 levels "2","2,1": 2 1
  > good <- read.table(header=TRUE, dec=",", text="x y\n1,1 2,1\n1 2\n")
  > str(good)
  'data.frame':   2 obs. of  2 variables:
   $ x: num  1.1 1
   $ y: num  2.1 2

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com


> -----Original Message-----
> From: [hidden email] [mailto:[hidden email]] On
> Behalf Of F86
> Sent: Wednesday, July 11, 2012 2:40 AM
> To: [hidden email]
> Subject: Re: [R] HELP me please with import of csv to R
>
> Thank you! It is fixed now.
>
> However, now when I'm trying with hist(skatter)   i get this message:  Error
> in hist.default(skatter) : 'x' must be numeric
>
> I don't know what I'm doing wrong but it worked perfectly on windows some
> weeks ago.
>
> My data skater looks like this:
>
> > skatter
>            Kommuner   Skatt
> 1          Botkyrka 32,23
> 2          Danderyd 29,45
> 3             Ekero 31,33
> 4           Haninge 31,88
> 5          Huddinge 32,05
> 6          Jarfalla 30,73
> 7           Lidingo 30,72
> 8             Nacka 30,71
> 9         Norrtalje 32,22
> 10          Nykvarn 32,05
> 11        Nynashamn 31,93
> 12            Salem    32
> 13          Sigtuna 32,08
> 14       Sollentuna  30,3
> -
> -
> -
> ..until 290
>
>
> Everything should be right since its a csv, the number of municipalities
> (Swedish: Kommuner) is 290 and all objects has a tax rate ( Skatt)
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Need-HELP-how-find-
> a-csv-file-tp4636019p4636119.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.
F86
Reply | Threaded
Open this post in threaded view
|

Re: HELP me please with import of csv to R

F86
Hello William,

- I used str()  and got this

'data.frame': 290 obs. of  2 variables:
 $ Kommuner: Factor w/ 289 levels "Ale","Alingsas",..: 34 40 44 79 95 99 132 162 169 173 ...
 $ Skatt   : Factor w/ 177 levels "28,89","28,9",..: 86 7 47 67 74 25 24 23 85 74 ...

 - and summary()

 - got this


 summary(skatter)
     Kommuner       Skatt    
 Habo    :  2   33,4   :  9  
 Ale     :  1   31     :  6  
 Alingsas:  1   32,45  :  6  
 Almhult :  1   32     :  5  
 Alvdalen:  1   32,15  :  5  
 Alvesta :  1   33,2   :  5  
 (Other) :283   (Other):254  



So obviously i have done something wrong. Do you know how i get it right?


Thanks!
Reply | Threaded
Open this post in threaded view
|

Re: HELP me please with import of csv to R

William Dunlap
You or nabble left out my suggestion to use dec="," in your
call to read.table (or read.csv, etc.)

  Did you use read.table(filename, ..., dec=",") when importing
  the data (so "30,3" is read as the number 30 and 3 tenths instead
  of as the character string "30,3")?

along with an example of what it did:

  > good <- read.table(header=TRUE, dec=",", text="x y\n1,1 2,1\n1 2\n")
  > str(good)
  'data.frame':   2 obs. of  2 variables:
   $ x: num  1.1 1
   $ y: num  2.1 2

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com


> -----Original Message-----
> From: [hidden email] [mailto:[hidden email]] On
> Behalf Of F86
> Sent: Wednesday, July 11, 2012 2:47 PM
> To: [hidden email]
> Subject: Re: [R] HELP me please with import of csv to R
>
> Hello William,
>
> - I used str()  and got this
>
> 'data.frame': 290 obs. of  2 variables:
>  $ Kommuner: Factor w/ 289 levels "Ale","Alingsas",..: 34 40 44 79 95 99 132
> 162 169 173 ...
>  $ Skatt   : Factor w/ 177 levels "28,89","28,9",..: 86 7 47 67 74 25 24 23
> 85 74 ...
>
>  - and summary()
>
>  - got this
>
>
>  summary(skatter)
>      Kommuner       Skatt
>  Habo    :  2   33,4   :  9
>  Ale     :  1   31     :  6
>  Alingsas:  1   32,45  :  6
>  Almhult :  1   32     :  5
>  Alvdalen:  1   32,15  :  5
>  Alvesta :  1   33,2   :  5
>  (Other) :283   (Other):254
>
>
>
> So obviously i have done something wrong. Do you know how i get it right?
>
>
> Thanks!
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Need-HELP-how-find-
> a-csv-file-tp4636019p4636204.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: HELP me please with import of csv to R

Daniel Nordlund-4
> -----Original Message-----
> From: [hidden email] [mailto:[hidden email]]
> On Behalf Of William Dunlap
> Sent: Wednesday, July 11, 2012 4:23 PM
> To: F86; [hidden email]
> Subject: Re: [R] HELP me please with import of csv to R
>
> You or nabble left out my suggestion to use dec="," in your
> call to read.table (or read.csv, etc.)
>
>   Did you use read.table(filename, ..., dec=",") when importing
>   the data (so "30,3" is read as the number 30 and 3 tenths instead
>   of as the character string "30,3")?
>
> along with an example of what it did:
>
>   > good <- read.table(header=TRUE, dec=",", text="x y\n1,1 2,1\n1 2\n")
>   > str(good)
>   'data.frame':   2 obs. of  2 variables:
>    $ x: num  1.1 1
>    $ y: num  2.1 2
>
> Bill Dunlap
> Spotfire, TIBCO Software
> wdunlap tibco.com
>
>
> > -----Original Message-----
> > From: [hidden email] [mailto:[hidden email]]
> On
> > Behalf Of F86
> > Sent: Wednesday, July 11, 2012 2:47 PM
> > To: [hidden email]
> > Subject: Re: [R] HELP me please with import of csv to R
> >
> > Hello William,
> >
> > - I used str()  and got this
> >
> > 'data.frame': 290 obs. of  2 variables:
> >  $ Kommuner: Factor w/ 289 levels "Ale","Alingsas",..: 34 40 44 79 95 99
> 132
> > 162 169 173 ...
> >  $ Skatt   : Factor w/ 177 levels "28,89","28,9",..: 86 7 47 67 74 25 24
> 23
> > 85 74 ...
> >
> >  - and summary()
> >
> >  - got this
> >
> >
> >  summary(skatter)
> >      Kommuner       Skatt
> >  Habo    :  2   33,4   :  9
> >  Ale     :  1   31     :  6
> >  Alingsas:  1   32,45  :  6
> >  Almhult :  1   32     :  5
> >  Alvdalen:  1   32,15  :  5
> >  Alvesta :  1   33,2   :  5
> >  (Other) :283   (Other):254
> >
> >
> >
> > So obviously i have done something wrong. Do you know how i get it
> right?
> >
> >
> > Thanks!
> >

And in an earlier post in this thread, Peter Dalgaard had pointed out the existence of read.csv2 for just this purpose.

Dan

Daniel Nordlund
Bothell, WA USA
 

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

Re: HELP me please with import of csv to R

F86
I have solved the problem. And as i assumed from the beginning it was a
simple one.

The problem was that i had my csv file on the descop. It should have been in
the working directory. So i just needed to read the file and then run lm()
like i wrote before.

Thanks to Ray DiGiacomo, Jr!


Regards,

Faradj



--
View this message in context: http://r.789695.n4.nabble.com/Need-HELP-how-find-a-csv-file-tp4636019p4650787.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.