[R] Using write.csv as a connection for read.csv

William Dunlap wdun|@p @end|ng |rom t|bco@com
Mon Jul 9 18:07:38 CEST 2018


 >I tried something like read.csv(write.csv(df,row.names=FALSE)) but got
the error
>
>  Error in read.table(file = file, header = header, sep = sep, quote =
quote,  :
>  'file' must be a character string or connection

To diagnose this without reading the help(write.csv) look at the return
value of write.csv:
  > df <- data.frame(Col1=1:3, Col2=LETTERS[24:26])
  > tmp <- write.csv(df, row.names=FALSE)
  "Col1","Col2"
  1,"X"
  2,"Y"
  3,"Z"
  > tmp
  NULL

read.csv complains about reading from NULL:
  > read.csv(NULL)
  Error in read.table(file = file, header = header, sep = sep, quote =
quote,  :
    'file' must be a character string or connection




Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Mon, Jul 9, 2018 at 7:42 AM, Kevin Thorpe <kevin.thorpe using utoronto.ca>
wrote:

> Hi.
>
> I have some data frames I created previously that seem to not be working
> correctly anymore. I *think* the problem is that some of the variables in
> the data frame are of a type called labelled. There are other attributes in
> the data frame as well. I thought that the easiest way to fix this was to
> convert to, say a csv and re-load.
>
> I tried something like read.csv(write.csv(df,row.names=FALSE)) but got
> the error
>
>   Error in read.table(file = file, header = header, sep = sep, quote =
> quote,  :
>   'file' must be a character string or connection
>
> I guess there must be a way to send the output of write.csv to a
> connection that read.csv can use but I was mystified by the help page on
> connections, at least I could not determine how to achieve my desired
> result.
>
> I realize I could write to a file and read it back in, but that feels
> klunky somehow. Maybe my approach to convert my data to strip the "weird"
> stuff is wrong-headed and I would accept alternative strategies.
>
> I would like a more general solution to fix this because I expect to
> encounter it some more. For those wondering how I found myself in such a
> mess, the data frames were initially imported from SAS data sets through
> the haven package. I then did some standard manipulation and added some
> additional labels with the upData() function from Hmisc (both packages have
> been updated since initial creation of the data frames).
>
> Thanks,
>
> Kevin
>
> --
>  Kevin E. Thorpe
>  Head of Biostatistics,  Applied Health Research Centre (AHRC)
>  Li Ka Shing Knowledge Institute of St. Michael's
>  Assistant Professor, Dalla Lana School of Public Health
>  University of Toronto
>  email: kevin.thorpe using utoronto.ca  Tel: 416.864.5776  Fax: 416.864.3016
>
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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]]




More information about the R-help mailing list