使用名称序列合并多个csv文件

编程入门 行业动态 更新时间:2024-10-24 08:24:27
本文介绍了使用名称序列合并多个csv文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试在R中合并700多个csv文件。我能够使用以下代码成功合并它们:

I am trying to merge 700+ csv files in R. I was able to merge them successfully using the code:

library(dplyr) library(readr) df <- list.files(full.names = TRUE) %>% lapply(read_csv) %>% bind_rows

现在,我的问题是文件名另存为flux.0,flux.1,flux。 2 ...... flux.733。 R会按flux.0,flux.1、1.flux.10,flux.100,flux.101 ...的顺序绑定文件。由于文件的顺序对我很重要,因此您可以建议将其合并到上面的代码中吗? 非常感谢您的帮助!

Now my problem is the file names are saved as flux.0, flux.1, flux.2......flux.733. The R binds the files in order of flux.0, flux.1, flux.10, flux.100, flux.101...and so on. Since the sequence of the file is important for me, can you suggest to incorporate this in the above code? Many Thanks for the help!

推荐答案

在这里,您将使用自己的方法得到另一个答案。我刚刚添加了一个读取csv的函数,并添加了一个名为'file'的新列,其文件名不带扩展名。

Here you have another answer using your own approach. I've just added a function that reads the csv and adds a new column called 'file' with the name of the file without the extension.

library(dplyr) library(readr) df <- list.files(full.names = TRUE) %>% lapply(function(x) {a <- read_csv(x); mutate(a, file = tools::file_path_sans_ext(basename(x)))}) %>% bind_rows

更多推荐

使用名称序列合并多个csv文件

本文发布于:2023-10-25 14:13:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1527193.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   序列   名称   文件   csv

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!