Changing names in a data list

I read all the text files in the working directory in the list and cut out several columns

all.files <- list.files(pattern = ".*.txt")
data.list <- lapply(all.files, function(x)read.table(x, sep="\t"))
names(data.list) <- all.files
data.list <- lapply(data.list, function(x) x[,1:3])

I get a "list of 2"

> str(data.list)
List of 2
 $ 001.txt:'data.frame':    71330 obs. of  3 variables:
  ..$ V1: Factor w/ 71321 levels
  ..$ V2: Factor w/ 1382 levels
  ..$ V3: num [1:71330] 89.1 99.5 98.8 99.4 99.5 ...
 $ 002.txt:'data.frame':    98532 obs. of  3 variables
  ..$ V1: Factor w/ 98517 levels 
  ..$ V2: Factor w/ 1348 levels 
  ..$ V3: num [1:98532] 99.5 99 99.5 98.4 100 ...

I want to rename V1, V2, V3 according to

new.names<-c("query", "sbjct", "ident")

How is this possible with the foot?

+4
source share
1 answer

You can try setNames

data.list <- lapply(data.list, setNames, new.names)
+5
source

All Articles