从列表中读取read.csv以获取唯一的名称 [英] read.csv from list to get unique colnames

查看:75
本文介绍了从列表中读取read.csv以获取唯一的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将文件读入 file_list .使用read.csv读取数据,但是,我希望 datalist 中的数据具有colnames作为 file_list 的文件名.原始文件没有标题.如何更改 function(x),以使第二列的名称与文件名相似.第一列不必是唯一的.

I am reading my files into file_list. The data is read using read.csv, however, I want the data in datalist to have colnames as the file-names the file_list. The original files does not have a header. How do I change function(x) so that the the second column has colname similar to the file-name. The first column does not have to be unique.

file_list = list.files(pattern="*.csv")     

datalist = lapply(file_list, function(x){read.csv(file=x,header=F,sep = "\t")})

推荐答案

如何更改功能(x),以使第二列具有与文件名相似的名称?

How do I change function(x) so that the the second column has colname similar to the file-name?

datalist = lapply(file_list, function(x){
    dat = read.csv(file=x, header=F, sep = "\t")
    names(dat)[2] = x
    return(dat)
})

这会将文件名作为第二列的名称.如果要编辑名称,请在 x 上使用 gsub substr (或类似名称)来修改字符串.

This will put the name of the file as the name of the second column. If you want to edit the name, use gsub or substr (or similar) on x to modify the string.

这篇关于从列表中读取read.csv以获取唯一的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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