rdataframer-rownames

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. 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. Because of this, I cannot do calculations between them. I want to do (table1 minus 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. 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


Solution

  • 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