R language | When exporting a data frame, there will be a problem that the first row is shifted to the left. The solution is as follows

When exporting the data frame, there will be a problem that the first row is shifted to the left by one bit. The solution is as follows

Notes:

"a": is the data frame we want to adjust

The idea of ​​the following two methods is essentially the same: extract the row names in the data frame a to become an independent column, and add it to the front of the original data frame a to become the new first column

(1) The first solution

adjustdata <- function(data) {
  data <- cbind(rownames(data),data) }  #自定义一个叫做adjustdata的函数,这两行代码直接照搬不用修改
a <- adjustdata (a)  #a是我们要导出的数据框,对它运算一下我们刚编写的这个函数
write.table(a, file = "a.csv", sep = ",", row.names = FALSE)  #最后输出文件

(2) The second solution

a <- data.frame(ID=rownames(a), a)  #这里的“ID”可以替换成任意名称,它的意思是你新创建的第一列的行名
write.table(a, "tax.csv", row.names=F, col.names = T, sep=",")

The effect is as follows:

 After doing the conversion:

insert image description here

 

Guess you like

Origin blog.csdn.net/tianyuu1/article/details/128105158