简体   繁体   English

在 R 中的多个 csv 文件中将唯一列分隔为多个列

[英]Separating unique column to multiple columns in multiple csv files in R

im trying to separate a unique column in multiple csv files.我试图在多个 csv 文件中分离一个唯一列。 I've already done it for one single file with this code:我已经使用以下代码为一个文件完成了它:

tempmax <- read.csv(file="path", header=TRUE, sep=";", fill = TRUE) 
colnames(tempmax) = c("Fecha", "Hora", "Temperatura max")
rbind(tempmax)
write.csv(tempmax, "path", sep = ";", append = FALSE, row.names = FALSE, col.names = FALSE)

However, I haven't found the way to do it in multiple csv saved in a folder.但是,我还没有找到保存在文件夹中的多个 csv 的方法。 I would like to do the same: read, modify and write the new one.我也想做同样的事情:阅读、修改和编写新的。

I used this to read the multiple files:我用它来读取多个文件:

getwd <- ("path")
filenames <- list.files("path", 
                        pattern = "*.csv", full.names = TRUE)

But i just cant find the way to edit what i want.但我只是找不到编辑我想要的东西的方法。 (i'm pretty new using R) I appreciate the help. (我是使用 R 的新手)感谢您的帮助。 Thanks!谢谢!

If we have several files, we can use lapply .如果我们有多个文件,我们可以使用lapply It is not clear about the transformation.转型还不是很清楚。 So, the file is written back by selecting the first column因此,通过选择第一列将文件写回

lapply(filenames, function(file){
      tempmax <- read.csv(file= file, header=TRUE, sep=";", fill = TRUE) 
      colnames(tempmax) = c("Fecha", "Hora", "Temperatura max")

      write.csv(tempmax[1], file, sep = ";", append = FALSE,
             row.names = FALSE, col.names = FALSE)})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM