将多个数据帧中的行名转换为数据帧中的列 [英] Convert row names in multiple data frames to column in data frame

查看:119
本文介绍了将多个数据帧中的行名转换为数据帧中的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表,我已经读入R并放在一个叫做$ code数据的大型数据框架中的 .csv 文件列表由6个data.frames组成,它们是文件名中的6个文件。到目前为止,我的代码是:

I have a list of .csv files that I have read in to R and placed in a large data frame called data that consists of 6 data.frames which are the 6 files in filenames. My code so far is:

filenames <- list.files( paste(mainDirInput,sep=""), pattern="Out.*csv", full.names=TRUE) 
data = lapply(filenames, function(f) {
wb = read.csv(f, header=TRUE)
})

每个数据框架中的行名和列名完全相同,我想提取行名,而将它们作为R中的第一列。我的一个数据框的一个例子如下所示:

The row names and column names in each data.frame are exactly the same, I would like to extract the row names and instead have them as the first column in R. An example of one of my data frames would be like this:

            w    x    y    z
2012 01     12   43   87   09
2012 02     14   53   75   76
2012 03     76   34   76   28
2012 04     41   36   85   16
  :         :    :    :    :
  :         :    :    :    :

我需要能够使用其他代码文件,所以我不能简单地创建一个新的列,值为 2012 01,2012 02,2012 03 ...

I need to be able to use this code on other files as well, so I can't simply just create a new column with the values 2012 01, 2012 02, 2012 03...

推荐答案

你走了t a dataframe ,其中列名为w,x,y,z。只需执行

Youve got a dataframe with columns named "w,x,y,z" . Just do

data$names <- rownames(data) 

添加一个新列。

这篇关于将多个数据帧中的行名转换为数据帧中的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆