R根据提供的索引值选择列的打印值

R根据提供的索引值选择列的打印值,r,ggplot2,plot,R,Ggplot2,Plot,我正试图找出如何以一种特殊的方式绘制一些值。假设我有以下示例数据: set.seed(100) test.df <- as.data.frame(matrix(1:36,nrow=6)) test.df$V7 <- sample(1:6,6) test.df$V8 <- seq(1:6) colnames(test.df) <- c("col1","col2","col3","col4","col5","col6","index","id") test.df c

我正试图找出如何以一种特殊的方式绘制一些值。假设我有以下示例数据:

set.seed(100)
test.df <- as.data.frame(matrix(1:36,nrow=6))
test.df$V7 <- sample(1:6,6)
test.df$V8 <- seq(1:6)
colnames(test.df) <- c("col1","col2","col3","col4","col5","col6","index","id")
test.df

    col1 col2 col3 col4 col5 col6 index id
1    1    7   13   19   25   31     2  1
2    2    8   14   20   26   32     6  2
3    3    9   15   21   27   33     3  3
4    4   10   16   22   28   34     1  4
5    5   11   17   23   29   35     4  5
6    6   12   18   24   30   36     5  6
我想通过使用索引列作为选择第1-6列的方法来绘制前6列的值。这就是y轴。x轴应该是id。基本上,第一个y值应该是7,因为索引选择第2列作为第一个值。第二个y值为32,因为索引值指示第6列


如果我能澄清任何其他问题,请告诉我。我对使用R ggplot2或其他工具绘图相当陌生,因此非常感谢您的帮助。

这不是ggplot2的问题

首先,您可以创建列“y”:

test.df[, "y"] <- 0
for (i in (1:nrow(test.df))) {
test.df[i, "y"] <- test.df[i, paste0("col", test.df[i, "index"])]
    }

谢谢你的回复。这解决了我的问题。
plot(y ~ id, data = test.df, type = "l")