0

I have just started using R and this might be a very basic thing, but here is my problem: I am trying to use a for loop to plot the first row and mean of the remaining rows for different dataframes, each one having its own plot. I first stored all my table names files <- c("file1, "file2", "file3"...)

Then I tried using a for loop to plot it for(i in files) {plot(c(1:13), i[1,])}

                    X    T     C    A  C.1  A.1   A.2     G   T.1  T.2   A.3  G.1   A.4
sample1            NA -0.6  10.6 13.7 -4.7  5.6 -11.7 -13.8 -11.9 -6.4 -12.7  2.9   7.5
sample2           3.4 -1.5  -1.3 11.9 10.3  8.0  -8.0  -9.9   6.5 -0.5   3.4 -4.5   4.8
sample3           0.8 -5.7   3.9 20.3 14.0  8.1  -8.0  -9.8   5.9 -6.7   6.5 -3.9   9.0
sample4          15.9 -6.1  -5.8 14.8 22.4 15.4  -2.8  -9.4   9.4 -4.2  -0.8  1.5   4.5

And that is where I am having trouble. I can plot it if I use the name of my file instead of i[1,], but I have many files. Each table looks like the following, but with more rows and columns. Also, does anyone know how to change the x axis so that instead of numbers and can assign a nucleotide letter to each position ? (X, T, C, A, C, A, A, G, T, T, A, G, A)

Any ideas? Thanks!

4

1 回答 1

0

您需要使用get(i)以引用由 给出的名称的对象i

例如:

for (i in files) {
  plot(1:ncol(get(i)), get(i)[1, ], xaxt='n')
  axis(1, at=1:ncol(get(i)), labels=colnames(get(i)))
}

默认 x 轴用 抑制xaxt='n',然后用 绘制axis(1, ...)。为了使代码能够泛化到data.frames不同数量的列,我使用1:ncol(get(i))了 ,它返回从 1 到具有 name 的对象的列数的整数序列i

于 2013-05-10T00:33:42.803 回答