Following is a function that I wrote (It is working well). It's a simple one, nothing complicated. The only question that I have is a qutation mark issue, I guess. ############################################# funcname <- function(trait.file){ #line1 setwd('/root/subroot') # line 2 load('imge.RData') # line3 imge <- imge[,-c(3)] # line4 imge <- imge[complete.cases(imge),] # line5 trait<- read.csv(trait.file) # line6 ngenes <- nrow(imge) # line7 nsnp <- nrow(trait) #line 8 for(i in 1:ngenes) { for(j in 1:nsnp) { if(imge$d1[i]==trait$d1[j] & imge$d2[i]==trait$d2[j]) trait$imgene2[j] <- imge$Gene[i] else trait$imgene2[j] <- NA } } return(trait) } hypertension <- funcname("folder/hyper.csv") # last line ###################################### 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 2) the code working: read.csv(trait.file) # line 8 funcname("folder/hyper.csv") # last line anyone can tell me why is the difference? 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.
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 lineThe 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
You need to understand the difference between a variable and a value. In your case that doesn't work, you are supplying a value, "trait.value". but you have no file named "trait.value". In the case that does work you are supplying a variable, trait.value, and the value contained in that variable is the name of a file that does exist. It is not quite correct to say that when using read.csv() you put quotation marks outside the filename. What you do is supply read.csv() with the name of a file, and that name can either be a character string, or a variable whose value is a character string. -Don On 7/16/10 8:23 AM, "karena" <dr.jzhou at gmail.com> wrote:> > Following is a function that I wrote (It is working well). It's a simple one, > nothing complicated. The only question that I have is a qutation mark issue, > I guess. > ############################################# > funcname <- function(trait.file){ #line1 > setwd('/root/subroot') # line 2 > load('imge.RData') # line3 > imge <- imge[,-c(3)] # line4 > imge <- imge[complete.cases(imge),] # line5 > trait<- read.csv(trait.file) # line6 > ngenes <- nrow(imge) # line7 > nsnp <- nrow(trait) #line 8 > for(i in 1:ngenes) { > for(j in 1:nsnp) { > if(imge$d1[i]==trait$d1[j] & imge$d2[i]==trait$d2[j]) trait$imgene2[j] <- > imge$Gene[i] > else trait$imgene2[j] <- NA > } > } > return(trait) > } > > hypertension <- funcname("folder/hyper.csv") # last line > ###################################### > > 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 > > 2) the code working: > read.csv(trait.file) # line 8 > funcname("folder/hyper.csv") # last line > > anyone can tell me why is the difference? > > thank you, > > karena >-- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory 925 423-1062