gpt4 book ai didi

r - R的格式化输出

转载 作者:行者123 更新时间:2023-12-04 11:33:20 25 4
gpt4 key购买 nike

我想在 GNU 绘图上绘制 R 的输出。例如,我有一个矩阵 x存储整数,我制作矩阵 y作为y<- x^2 .

现在 GNU plot 需要在输入数据文件中输入以下格式:

x1 y1
x2 y2
x3 y3
... and so on...

我如何从 R 中生成这个输出文件?

具体来说,如何删除 R 输出中显示的索引并垂直排列矩阵的元素?

最佳答案

我不太清楚你是想绘制两个向量的对比图,还是想用矩阵做点什么。我假设第一个。让我们创建一些示例数据:

x = 1:10
y = x^2
z = cbind(x, y)

接下来,我们将其放入文件中:

write.table(z, file = "/tmp/spam", row.names = FALSE, col.names = FALSE)

如果我们检查输出:

$ cat /tmp/spam
1 1
2 4
3 9
4 16
5 25
6 36
7 49
8 64
9 81
10 100

我认为这就是您所需要的。或者,只需执行以下操作:

plot(y~x, z, type = "l")

enter image description here

并完全避免使用 GNUPlot。或者更好的是,使用 ggplot2:

require(ggplot2); theme_set(theme_bw())
qplot(x, y, data = data.frame(z), geom = "line")

enter image description here

关于r - R的格式化输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13063246/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com