在r中读取csv时将列名添加到数据帧

时间:2017-11-17 05:54:07

标签: r csv dataframe multiple-columns read.csv

我的目录中有多个.csv文件没有列名。因此,在没有标题的情况下阅读它们会产生错误

  

match.names(clabs,names(xi))出错:     名称与以前的名称不匹配。

因此,我想将列名附加到那些csv文件并将它们全部合并到一个数据帧中,但是我无法在读取它们时为这些多个csv文件添加列名。文件名称类似于test_abc.csvtest_pqr.csvtest_xyz.csv等。 这是我试过的

temp = list.files(pattern="*.csv")
read_csv_filename <- function(filename){
  ret <- read.csv(filename,header = F)
  ret$city <- gsub(".*[_]([^.]+)[.].*", "\\1", filename) 
  ret
}

df_all <- do.call(rbind,lapply(temp,read_csv_filename))

如何在阅读时在此处为每个文件添加标题?

这是我想在阅读时添加的名称

colnames = c("Age","Gender","height","weight")

有什么建议吗?

2 个答案:

答案 0 :(得分:1)

使用tidyverse包,您可以使用purrr::map_dfr函数很好地执行此操作,该函数迭代列表,对每次返回数据帧的每个元素执行一些函数,并对所有这些进行行绑定数据框一起。


library(readr)
library(purrr)
library(dplyr) # only used in example set up

# Setting up some example csv files to work with

mtcars_slim <- select(mtcars, 1:3)

write_csv(slice(mtcars_slim, 1:4), "mtcars_1.csv", col_names = FALSE)
write_csv(slice(mtcars_slim, 5:10), "mtcars_2.csv", col_names = FALSE)
write_csv(slice(mtcars_slim, 11:1), "mtcars_3.csv", col_names = FALSE)


# get file paths, read them all, and row-bind them all

dir(pattern = "mtcars_\\d+\\.csv") %>% 
  map_dfr(read_csv, col_names = c("mpg", "cyl", "disp"))

#> Parsed with column specification:
#> cols(
#>   mpg = col_double(),
#>   cyl = col_integer(),
#>   disp = col_integer()
#> )

#> # A tibble: 21 x 3
#>      mpg   cyl  disp
#>    <dbl> <int> <dbl>
#>  1  21.0     6 160.0
#>  2  21.0     6 160.0
#>  3  22.8     4 108.0
#>  4  21.4     6 258.0
#>  5  18.7     8 360.0
#>  6  18.1     6 225.0
#>  7  14.3     8 360.0
#>  8  24.4     4 146.7
#>  9  22.8     4 140.8
#> 10  19.2     6 167.6
#> # ... with 11 more rows

答案 1 :(得分:0)

你可以将colnames放在循环中,就像这样

template <typename T>
struct Traits { /* maybe some default values */ };

template<typename T>
class A{
    using type = typename Traits<T>::type;
    type someFunction(/*some parameters*/){/*code for the function*/}
    //other stuff
};

template<typename T>
class B : public A<B<T>>{
    using type = typename Traits<B<T>>::type;
};

template <typename T>
struct Traits<B<T>> {
    using type = T;
};