[R] a issue about the qutation mark?

Ista Zahn izahn at psych.rochester.edu
Sat Jul 17 00:01:32 CEST 2010


Hi Karena,

On Fri, Jul 16, 2010 at 11:23 AM, karena <dr.jzhou at gmail.com> wrote:
<snip>

> So, as we all know, when using "read.csv", we need to use qutation mark out
> side the filename which we wanna read in. At first, for line 8, I wrote:
> read.csv("trait.file"), at last line, I wrote:
> funcname(folder/hyper.csv), but it did not work in this way. Unless I
> changed the code to the current one that I showed above, I couldn't get what
> I want.
> So, to be straightforward, I will show the difference:
> 1) the code not working:
> read.csv("trait.file")       #line 8
> funcname(folder/hyper.csv)  #last line

The problem here is that unquoted commands used to refer to objects
saved in your R working environment (well I may not be using the right
terminology here. But I hope it is clear enough). Also, the /
character is an arithmetic function.  So when you call
funcname(folder/hyper.csv) R will look for an object named folder and
try to divide it by an object named hyper.csv). This could sometimes
make sense, e.g.,

test.fun <- function(x)
{
  (sqrt(x))
}

folder <- 8
hyper.csv <- 2

test.fun(folder/hyper.csv)

but it is not what you want here. In your case, you need to pass a
string to the read.csv function containing the filename.

Even if you got passed this problem, you have another one, which is
that by quoting the argument in read.csv("trait.file") you are asking
read.csv to find a file located in the current working directory named
"trait.file". This is not what you want. You want read.csv to find a
file specified by the argument to the funcname() function.

>
> 2) the code working:
> read.csv(trait.file)         # line 8
> funcname("folder/hyper.csv")   # last line
>
> anyone can tell me why is the difference?

This works because you are correctly passing a string to the
read.csv() function. Maybe it helps to try this:

dat <- data.frame(a=rnorm(10), b=rnorm(10))
write.csv(dat, file="test.csv")
file.name <- "test.csv"
dat2 <- read.csv("file.name")
dat2 <- read.csv(file.name)

Best,
Ista

>
> thank you,
>
> karena
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/a-issue-about-the-qutation-mark-tp2291537p2291537.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.
>



-- 
Ista Zahn
Graduate student
University of Rochester
Department of Clinical and Social Psychology
http://yourpsyche.org



More information about the R-help mailing list