[R] Errors, driving me nuts
Paul Hiemstra
paul.hiemstra at knmi.nl
Tue Aug 2 14:43:38 CEST 2011
On 08/01/2011 08:47 PM, Matt Curcio wrote:
> Greetings all,
> I am getting this error that is driving me nuts... (not a long trip, haha)
>
> I have a set of files and in these files I want to calculate ttests on
> rows 'compareA' and 'compareB' (these will change over time there I
> want a variable here). Also these files are in many different
> directories so I want a way filter out the junk... Anyway I don't
> believe that this is related to my errors but I mention it none the
> less.
>
>> files_to_test <- list.files (pattern = "kegg.combine")
>> for (i in 1:length (files_to_test)) {
> + raw_data <- read.table (files_to_test[i], header=TRUE, sep=" ")
> + tmpA <- raw_data[,compareA]
> + tmpB <- raw_data[,compareB]
> + tt <- t.test (tmpA, tmpB, var.equal=TRUE)
> + tt_pvalue[i] <- tt$p.value
> + }
> Error in tt_pvalue[i] <- tt$p.value : object 'tt_pvalue' not found
> # I tried setting up a vector...
> # as.vector(tt_pvalue, mode="any") ### but NO GO
...an awesome alternative is to use ldply from the plyr package:
library(plyr)
files_to_test <- list.files (pattern = "kegg.combine")
tt_pvalue <- ldply(files_to_test, function(fname) {
raw_data <- read.table (files_to_test[i], header=TRUE, sep=" ")
tmpA <- raw_data[,compareA]
tmpB <- raw_data[,compareB]
tt <- t.test (tmpA, tmpB, var.equal=TRUE)
return(data.frame(fname = fname, pvalue = tt$p.value))
}, .progress = TRUE)
This saves you some bookkeeping (no need to create tt_pvalue in advance
and keep track of the iterator (i)) and you get a nice progress bar
(good when loops take long). ldply (and other plyr functions) are what I
use most when processing large amounts of information.
cheers,
Paul
>> file.name = paste("ttest.results.", compareA, compareB, "")
>> setwd(save_to)
>> write.table(tt_pvalue, file=file.name, sep="\t" )
> Error in inherits(x, "data.frame") : object 'tt_pvalue' not found
> # No idea??
>
> What is going wrong??
> M
>
>
> Matt Curcio
> M: 401-316-5358
> E: matt.curcio.ri at gmail.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.
--
Paul Hiemstra, Ph.D.
Global Climate Division
Royal Netherlands Meteorological Institute (KNMI)
Wilhelminalaan 10 | 3732 GK | De Bilt | Kamer B 3.39
P.O. Box 201 | 3730 AE | De Bilt
tel: +31 30 2206 494
http://intamap.geo.uu.nl/~paul
http://nl.linkedin.com/pub/paul-hiemstra/20/30b/770
More information about the R-help
mailing list