Achak
Achak

Reputation: 1296

Changing coloumn names for all the files in the working directory

I am trying to learn how to work with multiple files, I have 5 sample csv files in the working directory that I am reading with the following codes:

j = list.files()
d = lapply(j, read.csv, skip=6)

each files has 27 columns and I am trying to set column name for each file, I know how to set column name for a individual file, for example :

colnames(data) = c("type","date","v1","v2","v3","v4","v5","v6","v7","v8","v9","v10","v11","v12","v13","v14","v15","v16","v17","v18","v19","v20","v21","v22","v23","v24","total")

I am just wondering how can I set for all the the files in the directory?

many thanks, Ayan

Upvotes: 0

Views: 89

Answers (1)

Justin
Justin

Reputation: 43255

lapply will work again:

a <- data.frame(x=1:3, y=4:6)
my.list <- list(a,a)
lapply(my.list, function(x) {names(x) <- c('a', 'b') ; return(x)})

Upvotes: 2

Related Questions