Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing names in a list of dataframes [duplicate]

Tags:

r

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?

like image 883
nouse Avatar asked Mar 09 '15 14:03

nouse


1 Answers

You can try setNames

data.list <- lapply(data.list, setNames, new.names)
like image 153
akrun Avatar answered Oct 19 '22 21:10

akrun