I read all textfiles in the working directory into a list, and cut some 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 am ending up with 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 lapply?
You can try setNames
data.list <- lapply(data.list, setNames, new.names)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With