Marcos Amaris Gonzalez
2011-Mar-31 05:24 UTC
[R] Assign Names of columns in data.frame dinamically
Hello List. I have many files of ECG, each one with 7 column and I need only the second column and the name of each ECG. I am doing this but althought I have various days trying this i haven't gotten, so I ask help with this, if somebody cans help me I'll be so thankfully. -------------------------------------------------------------------------------------------------------- rm(list=ls()) # loadEcgFiles <- function(Dir=".") { Dir <- "."; txtfiles <- list.files(paste(Dir),'.txt$'); ecg = data.frame(ncol=1); len = length(txtfiles); for (i in 1:5 ) { # i <- 1; filename = paste(projectDir, "/" , txtfiles[i],sep=''); sample = read.table(filename, nrow=75000); sampleName = filename; sampleName=gsub("./", "",sampleName); sampleName=gsub(".txt", "", sampleName); temp <- sample$V2; names(temp) <- sampleName; ecg = cbind(ecg, temp); # } Thanks and sorry for my english. --- Marcos Amaris Gonz?lez - Linux Counter #462840 ------------------------------------------------------------------------------ No al SPAM! "No es m?s rico el que m?s tiene, sino el que menos necesita." ------------------------------------------------------------------------------
Hi Marcos, I'd be glad to help, but your example is not usable for anyone but yourself - I don't have the files that your code reads and I don't have any idea what the resulting data frames look like. Please provide examples of the data and the result. Andreas Marcos Amaris Gonzalez schrieb:> Hello List. > > I have many files of ECG, each one with 7 column and I need only the > second column and the name of each ECG. I am doing this but althought > I have various days trying this i haven't gotten, so I ask help with > this, if somebody cans help me I'll be so thankfully. > > -------------------------------------------------------------------------------------------------------- > rm(list=ls()) > > # loadEcgFiles <- function(Dir=".") { > Dir <- "."; > > txtfiles <- list.files(paste(Dir),'.txt$'); > ecg = data.frame(ncol=1); > len = length(txtfiles); > > for (i in 1:5 ) { > # i <- 1; > filename = paste(projectDir, "/" , > txtfiles[i],sep=''); > sample = read.table(filename, nrow=75000); > sampleName = filename; sampleName=gsub("./", > "",sampleName); sampleName=gsub(".txt", "", sampleName); > temp <- sample$V2; > names(temp) <- sampleName; > ecg = cbind(ecg, temp); > # } > > Thanks and sorry for my english. > > --- > Marcos Amaris Gonz?lez - Linux Counter #462840 > ------------------------------------------------------------------------------ > No al SPAM! > "No es m?s rico el que m?s tiene, sino el que menos necesita." > ------------------------------------------------------------------------------ > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > >-- Andreas Borg Medizinische Informatik UNIVERSIT?TSMEDIZIN der Johannes Gutenberg-Universit?t Institut f?r Medizinische Biometrie, Epidemiologie und Informatik Obere Zahlbacher Stra?e 69, 55131 Mainz www.imbei.uni-mainz.de Telefon +49 (0) 6131 175062 E-Mail: borg at imbei.uni-mainz.de Diese E-Mail enth?lt vertrauliche und/oder rechtlich gesch?tzte Informationen. Wenn Sie nicht der richtige Adressat sind oder diese E-Mail irrt?mlich erhalten haben, informieren Sie bitte sofort den Absender und l?schen Sie diese Mail. Das unerlaubte Kopieren sowie die unbefugte Weitergabe dieser Mail und der darin enthaltenen Informationen ist nicht gestattet.