在之前的消息中 Convert table into matrix by column names
我想对 csv 表或 R 中的表使用相同的方法。您能教我如何修改第一个命令行吗?
x <- read.table(textConnection(' models cores time 4 1 0.000365 4 2 0.000259 4 3 0.000239 4 4 0.000220 8 1 0.000259 8 2 0.000249 8 3 0.000251 8 4 0.000258' ), header=TRUE)
library(reshape) cast(x, models ~ cores)
我应该对 data.csv 文件使用以下内容吗
x <- read.csv(textConnection("data.csv"), header=TRUE)
我是否应该将以下内容用于名为 xyz 的 R 表
x <- xyz(textConnection(xyz), header=TRUE)
是否必须要有 textConnection 才能使用 cast 命令?
谢谢。
最佳答案
几年后...
read.table
及其衍生物,如 read.csv
现在有一个 text
争论,所以你不需要搞乱 textConnection
直接了。
read.table(text = "
x y z
1 1.9 'a'
2 0.6 'b'
", header = TRUE)
主要用于textConnection
是当在 SO 上提问的人只是将他们的数据转储到屏幕上,而不是编写代码让回答者自己生成它。例如,
Blah blah blah I'm stuck here is my data plz help omg
x y z
1 1.9 'a'
2 0.6 'b'
etc.
在这种情况下,您可以从屏幕上复制文本并将其包装在对 textConnection
的调用中,像这样:
the_data <- read.table(tc <- textConnection("x y z
1 1.9 'a'
2 0.6 'b'"), header = TRUE); close(tc)
当提问者提供代码时会更好,就像这样:
the_data <- data.frame(x = 1:2, b = c(2.9, 0.6), c = letters[1:2])
当您使用自己的数据时,您永远不需要使用 textConnection
.
my_data <- read.csv("my data file.csv")
应该足够了。
关于R:使用 textconnection 命令加载 R 表或 csv 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5624369/