Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用r中的名称列表重命名多个文件的特定列?_R_List - Fatal编程技术网

如何使用r中的名称列表重命名多个文件的特定列?

如何使用r中的名称列表重命名多个文件的特定列?,r,list,R,List,我有一个.txt文件文件夹。我已通过以下命令将文件读入R中的列表: filenames <- list.files("/path/to/folder") datalist = lapply(filenames, function(x)read.table(x, header=T)) filenames您可以通过子集colnames并插入以下示例中的类似内容,按位置更改单个列名 lst <- list(data.frame('a' = c(1,2,3),

我有一个.txt文件文件夹。我已通过以下命令将文件读入R中的列表:

filenames <- list.files("/path/to/folder")
datalist = lapply(filenames, function(x)read.table(x, header=T))

filenames您可以通过子集
colnames
并插入以下示例中的类似内容,按位置更改单个列名

lst <- list(data.frame('a' = c(1,2,3),
                       'b' = c(3,5,6)),
            data.frame('a' = c(1,2,3),
                       'b' = c(3,5,6)),
            data.frame('a' = c(1,2,3),
                       'b' = c(3,5,6)))

lapply(lst, function(item) {
    colnames(item)[2] <- 'cat'   # This would be 9 if you wanted the 9th column
    return(item)
})

[[1]]
  a cat
1 1   3
2 2   5
3 3   6

[[2]]
  a cat
1 1   3
2 2   5
3 3   6

[[3]]
  a cat
1 1   3
2 2   5
3 3   6
lst