通过read.clipboard将excel中的数据导入到R中

我想简化一个stream程,我从Excel工作表中select并复制两列,然后将它们导入到R中,在那里进一步对它们进行子集化。 这是我的问题:

excel数据在同一列中有多组数据。 例如:第1列是[V,1,2,3,4,V,1,2,3,4],第2列是[A,2,4,6,10,A,3,6,9 ,12]其中V和A是列标题。 我试着复制两个相关的列,然后在R中运行下面的代码:

testing<-read.clipboard(header=TRUE, sep=" ") testinga<-testing[1:4,] 

结果表看起来不错,但当在ggplot中绘制

  ggplot(testing, aes(V,A))+geom_point() 

结果图表按第一个数字sorting我的数据点(即10绘制为1)

这不是一个问题,如果我只是复制第一个数据集,并使用read.clipboard导入它

这里发生了什么,我该如何解决这个问题?

编辑:

 # from dput() testing <- structure(list(V = structure(c(1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L), .Label = c("1", "2", "3", "4", "V"), class = "factor"), A = structure(c(3L, 5L, 6L, 1L, 8L, 4L, 6L, 7L, 2L), .Label = c("10", "12", "2", "3", "4", "6", "9", "A"), class = "factor")), .Names = c("V", "A"), class = "data.frame", row.names = c(NA, -9L)) 

你的问题是,如果除了数字之外还有其他东西,比如更多的列名,那么大数据框的列会被转换成因子(而不是数字)。 你只需要转换回数字。

 testinga <- testing[1:4, ] testinga <- sapply(testinga, FUN = function(x){as.numeric(as.character(x))}) 

那么你应该可以把情况弄好。