2012-03-02 27 views
0

我有2个文件包含2个不同主机上相同测试的基准测试结果。结果是空间分隔格式的原始数据。使用R比较来自2个文件的数据使用R

我需要能够比较他们彼此作为主机是不一样的。主要用于绘图的目的。有没有办法添加每个文件独有的“字段”或列(但与该文件中的所有行相同),然后我可以使用它来区分图表中的结果?我还能如何去做这件事。

回答

3

你可以明确地添加额外的列。

例如:

# first file 
df1 <- read.table(...) 
# identify as first file 
df1$file_name <- 'file1' 

# second file 
df2 <- read.table(...) 
df2$file_name <- 'file2' 

# combine: 
df <- rbind(df1,df2) 

当然,你不需要做这么多的步骤,但是这应该给你一个起点方向。

+0

这是抛出一个错误:“错误match.names(clabs,姓名(十一)): 名称不匹配以前的名字” – Jericon 2012-03-08 01:27:03

+0

这是因为你的两个数据框必须有不同的列名 - 您的文件1和文件2具有不同的列名称。在合并之前将它们设置为相同的(例如,如果'df1'具有列'a'和'c',而'df2'具有列'a'和'b',则R不会将'b'合并到同一列as'c'因为它们可能有不同的列名,因为它们不应该组合)。 – 2012-03-08 01:35:41

+0

他们有相同的列名。但是我已经在rbind之后定义了列的名称。在修正它之前移动它。谢谢! – Jericon 2012-03-08 02:01:06

1

这里的总体思路:

# Some example data 
d1 <- read.table(text = " 
a b 
1 2 
2 8 
3 4", header=T) 

d2 <- read.table(text = " 
a b 
1 3 
2 10 
3 5", header=T) 

# Add an identifying column to each data.frame, then 'rbind()' them together 
d1 <- data.frame(host = "host1", d1) 
d2 <- data.frame(host = "host2", d2) 
d <- rbind(d1, d2) 

# Plot the results with your graphical system of choice 
library(lattice) 
xyplot(b~a, group=host, data=d, type="b", auto.key=TRUE)