简体   繁体   中英

R multi-file operation

First I load table from the txt file and then I perform several transformations on the table:

directory <- 'path'

setwd(directory)


data <- read.table("sample.txt", header=TRUE)

data2 <- data[,c(1,2,3,5,7,8)]

data2$cb <- paste(data2$c,data2$b,sep = ".")

finaldata <- data2[,c(7,1,2,3,4,5,6)]

The problem is that I have about 100 of such txt files and I need to automate that process.

I start by listing files like this:

filenames <- list.files(path=getwd(),pattern = "*.txt")

But now I dont have any idea how it should work. Should i do it in for loop or is there any other way?

Im new in R and this is my first post, so I ask for your understanding.

for (i in 1:length(filenames)) {

  data <- read.table(filenames[i], header=TRUE)

  ...


}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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