[英]How to create a dataset from an Excel containing row and column names in R?
I have 2 .csv files that are transformed from Excel tables. 我有2个从Excel表转换的.csv文件。 Their row and column names are same but they contain different data. 它们的行名和列名相同,但是包含不同的数据。 When I write table1<-read.csv("table1.csv",header=TRUE)
and table2<-read.csv("table2.csv",header=TRUE)
, row names are not names of rows in dataframe. 当我写table1<-read.csv("table1.csv",header=TRUE)
和table2<-read.csv("table2.csv",header=TRUE)
,行名称不是数据帧中行的名称。 Because of this, I cannot do calculations between them. 因此,我无法在它们之间进行计算。 I want to do (table1 minus table2). 我想做(table1减去table2)。 Accually, their row and column names are string but in the dataframe row names are 1,2,3,...Because the row names look string in the dataframe, calculations cannot be done. 准确地说,它们的行名和列名是字符串,但是在数据框中,行名是1,2,3,...。由于行名在数据帧中看起来是字符串,因此无法进行计算。 What should I do? 我该怎么办?
table1<-read.csv("table1.csv",header=TRUE)
table2<-read.csv("table2.csv",header=TRUE)
diff<-table1-table2
I have this message: Warning message: In Ops.factor(left, right) : '-' not meaningful for factors 我收到此消息:警告消息:在Ops.factor(left,right)中:'-'对因子没有意义
By what you said, first column in both data frames is row names and in string format. 用您所说的话,两个数据框中的第一列都是行名和字符串格式。 So leaving first column in both data frames, subtraction must be done. 因此,在两个数据帧中都保留第一列,必须进行减法运算。
table1 <- read.csv("table1.csv",header=TRUE)
table2 <- read.csv("table2.csv",header=TRUE)
diff <- cbind(table1[1], table1[-1]-table2[-1])
cbind here combines column one from table1 and result, keeping row name in data frame 这里的cbind结合了table1的第一列和结果,将行名保留在数据框中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.