0

I am trying to write code that takes values from one column of each of many files and prints out a list of the values of a different column depending on the values found in the first. If that makes sense. I have read the files in, but I am having trouble managing the table. I would like to limit the table to just those two columns, because the files are very large, cumbersome and unnecessary. In my attempt to do so I had this line:

tmp<-stack(lapply(inputFiles,function(x) x[,3]))

But ideally I would like to include two columns (3 and 1), not just one, so that I may use a line, such as these ones:

search<-tmp[tmp$values < 100, "Target"]
write(search, file = "Five", ncolumns = 2)

But I am not sure how. I am almost certain that stack is not going to work for more than one column. I tried some different things, similar to this:

tmp<-stack(lapply(inputFiles,function(x) x[,3], x[,1]))

But of course that didn't work.

But I don't know where to look. Does anyone have any suggestions?

Stephopolis
  • 1,765
  • 9
  • 36
  • 65
  • 1
    If you provide a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) I could test my answer :-). Copy/pasting the output of `dput(lapply(inputFiles,function(x) x[,3]))` should do it. – Ari B. Friedman Aug 20 '12 at 22:35

1 Answers1

1

The taRifx package has a list method for stack that will do what you want. It stacks lists of data.frames.

Untested code:

library(taRifx)
tmp<-stack(lapply(inputFiles,function(x) x[,c(1,3)]))

But you didn't change anything! Why does this work?

lapply() returns a list. In your case, it returns a list where each element is a data.frame.

Base R does not have a special method for stacking lists. So when you call stack() on your list of data.frames, it calls stack.default, which doesn't work.

Loading the taRifx library loads a method of stack that deals specifically with lists of data.frames. So everything works fine since stack() now knows how to properly handle a list of data.frames.

Tested example:

dat <- replicate(10, data.frame(x=runif(2),y=rnorm(2)), simplify=FALSE)
str(dat)
stack(dat)

            x           y
1  0.42692948  0.32023455
2  0.75388820  0.24154125
3  0.64035957  1.96580059
4  0.47690790 -1.89772855
5  0.41668993  0.78083412
6  0.12643784  0.38029833
7  0.01656855  0.51225268
8  0.40653094  1.09408159
9  0.94236491 -0.13410923
10 0.05578115  1.12475364
11 0.75651062 -0.65441493
12 0.48210444  1.67325343
13 0.95348755  0.04828449
14 0.02315498 -0.28481193
15 0.27370762  0.43927826
16 0.83045889  0.75880763
17 0.40049367  0.06945058
18 0.86212662  1.49918712
19 0.97611629  0.13959291
20 0.29107186  0.64483646
Ari B. Friedman
  • 71,271
  • 35
  • 175
  • 235