简体   繁体   English

如何从包含R中行和列名称的Excel创建数据集?

[英]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.

相关问题 如何通过 r 中的选定行创建新列并添加列名 - How create new column an add column names by selected row in r 如何按列名和行名对单元格的值求和以在 R 中创建矩阵? - How to sum values of cells by their column and row names to create a matrix in R? 根据 R 中的条件,使用包含列名的字符串创建新列 - Create new column with string containing column names based on condition in R 如果指定值与使用 R 的行相对应,如何将宽数据集中的列名作为长数据集中的行 - How to bring column name from wide dataset as row in long dataset if specified value corresponded with row using R 如何从包含数据和名称的数据框中创建具有指定行和列名的矩阵 - How to create a matrix with specified row and col names, from a dataframe containing the data and the names 如何使用R中数据集中列的变量创建表? - How to create a table by using the variables of a column from a dataset in R? R:创建行名和列名组合的列表,并对它们进行排名 - R: Create a list of combinations of row names and column names and rank them 在 R 中,如何使用行名和列名创建包含在“矩阵”或“数据框”设置中的图? - In R, how can I create plots contained within a “matrix” or “dataframe” setting with row names and column names? R,没有列名的数据集 - R, Dataset without column names Pivot 数据集和列名称 [R] - Pivot dataset and column names [R]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM