[R] Rbind with data frames -- column names question
As part of my work, I am trying to append matrices onto data frames. Naively I assumed that when rbinding a data.frame and matrix, the matrix would be coerced and appended, keeping the names from the data frame. Clearly, I am not fully understanding the process by which rbind works. Example code: A-data.frame(1,1,1); names(A)=letters[1:3] ; B-matrix(0,2,3) rbind(A,B) Error in match.names(clabs, names(xi)) : names do not match previous names: V1, V2, V3 rbind(A,as.data.frame(B)) Error in match.names(clabs, names(xi)) : names do not match previous names: V1, V2, V3 Is there a right way to combine the two such that the both end up having the same column names? I have tried to understand the deparse.level argument of rbind, but it doesn't seem to do what I'm asking. Thank you for any help you can give. Gregg -- Gregg Lind, M.S. Division of Epidemiology and Community Health School of Public Health University of Minnesota, United States __ R-help@stat.math.ethz.ch 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.
[R] Rbind with data frames -- column names question
Gregg, What about A-data.frame(1,1,1); names(A)=letters[1:3] ; B-matrix(0,2,3) B-as.data.frame(B) names(B)-names(A) rbind(A,B) -Cody Gregg Lind [EMAIL PROTECTED] To Sent by: r-help@stat.math.ethz.ch [EMAIL PROTECTED] cc at.math.ethz.ch Subject [R] Rbind with data frames -- 03/05/2007 02:06 column names question PM As part of my work, I am trying to append matrices onto data frames. Naively I assumed that when rbinding a data.frame and matrix, the matrix would be coerced and appended, keeping the names from the data frame. Clearly, I am not fully understanding the process by which rbind works. Example code: A-data.frame(1,1,1); names(A)=letters[1:3] ; B-matrix(0,2,3) rbind(A,B) Error in match.names(clabs, names(xi)) : names do not match previous names: V1, V2, V3 rbind(A,as.data.frame(B)) Error in match.names(clabs, names(xi)) : names do not match previous names: V1, V2, V3 Is there a right way to combine the two such that the both end up having the same column names? I have tried to understand the deparse.level argument of rbind, but it doesn't seem to do what I'm asking. Thank you for any help you can give. Gregg -- Gregg Lind, M.S. Division of Epidemiology and Community Health School of Public Health University of Minnesota, United States __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] Rbind with data frames -- column names question
This takes care of things quite nicely. The other solutions (explicitly coercing things) work as well, but this seems to me the minimum necessary solution for my particular problem. (P.s.: I hope I responded in the correct way to ensure threading... to the main list address.) A- data.frame(1,1,1); names(A)=letters[1:3] ; B-matrix(0,2,3) colnames(B) - colnames(A) rbind(A,B) GL jim holtman wrote: colnames(B) - colnames(A) On 3/5/07, *Gregg Lind* [EMAIL PROTECTED] mailto:[EMAIL PROTECTED] wrote: As part of my work, I am trying to append matrices onto data frames. Naively I assumed that when rbinding a data.frame and matrix, the matrix would be coerced and appended, keeping the names from the data frame. Clearly, I am not fully understanding the process by which rbind works. Example code: A- data.frame(1,1,1); names(A)=letters[1:3] ; B-matrix(0,2,3) rbind(A,B) Error in match.names(clabs, names(xi)) : names do not match previous names: V1, V2, V3 rbind(A,as.data.frame(B)) Error in match.names(clabs, names(xi)) : names do not match previous names: V1, V2, V3 Is there a right way to combine the two such that the both end up having the same column names? I have tried to understand the deparse.level argument of rbind, but it doesn't seem to do what I'm asking. Thank you for any help you can give. Gregg -- Gregg Lind, M.S. Division of Epidemiology and Community Health School of Public Health University of Minnesota, United States __ R-help@stat.math.ethz.ch mailto:R-help@stat.math.ethz.ch 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. -- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve? [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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.
Re: [R] Rbind with data frames -- column names question
I don't know if this is the right way, but I think this would work (I'm assuming you want the result to be a data frame): data.frame(rbind(as.matrix(A),B)) You might get a warning about row names, but I think it works OK. On 05/03/07, Gregg Lind [EMAIL PROTECTED] wrote: As part of my work, I am trying to append matrices onto data frames. Naively I assumed that when rbinding a data.frame and matrix, the matrix would be coerced and appended, keeping the names from the data frame. Clearly, I am not fully understanding the process by which rbind works. Example code: A-data.frame(1,1,1); names(A)=letters[1:3] ; B-matrix(0,2,3) rbind(A,B) Error in match.names(clabs, names(xi)) : names do not match previous names: V1, V2, V3 rbind(A,as.data.frame(B)) Error in match.names(clabs, names(xi)) : names do not match previous names: V1, V2, V3 Is there a right way to combine the two such that the both end up having the same column names? I have tried to understand the deparse.level argument of rbind, but it doesn't seem to do what I'm asking. Thank you for any help you can give. Gregg -- Gregg Lind, M.S. Division of Epidemiology and Community Health School of Public Health University of Minnesota, United States __ R-help@stat.math.ethz.ch 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. -- = David Barron Said Business School University of Oxford Park End Street Oxford OX1 1HP __ R-help@stat.math.ethz.ch 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.