[R] dataframe layout
Can someone remind me how to change the columns in df.a into a two column df.b that contains one column of data and another column of the original column headings as levels. Example: a=1:3 b=4:6 c=7:9 df.a=data.frame(a,b,c) Should become in df.b: dat lev 1 a 2 a 3 a 4 b 5 b 6 b 7 c 8 c 9 c Thanks. __ 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] dataframe layout
On Wed, 2007-03-14 at 03:53 -0500, Robert Baer wrote: Can someone remind me how to change the columns in df.a into a two column df.b that contains one column of data and another column of the original column headings as levels. Example: a=1:3 b=4:6 c=7:9 df.a=data.frame(a,b,c) Should become in df.b: dat lev 1 a 2 a 3 a 4 b 5 b 6 b 7 c 8 c 9 c Thanks. One option is stack() a=1:3 b=4:6 c=7:9 df.a=data.frame(a,b,c) df.a a b c 1 1 4 7 2 2 5 8 3 3 6 9 stack(df.a) values ind 1 1 a 2 2 a 3 3 a 4 4 b 5 5 b 6 6 b 7 7 c 8 8 c 9 9 c class(stack(df.a)) [1] data.frame HTH G -- %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% Gavin Simpson [t] +44 (0)20 7679 0522 ECRC [f] +44 (0)20 7679 0565 UCL Department of Geography Pearson Building [e] gavin.simpsonATNOSPAMucl.ac.uk Gower Street London, UK[w] http://www.ucl.ac.uk/~ucfagls/ WC1E 6BT [w] http://www.freshwaters.org.uk/ %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~% __ 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] dataframe layout
Robert Baer wrote: Can someone remind me how to change the columns in df.a into a two column df.b that contains one column of data and another column of the original column headings as levels. Example: a=1:3 b=4:6 c=7:9 df.a=data.frame(a,b,c) Should become in df.b: dat lev 1 a 2 a 3 a 4 b 5 b 6 b 7 c 8 c 9 c Here are a couple of different approaches: df.b - data.frame(dat = unlist(df.a), lev = rep(names(df.a), each = dim(df.a)[1])) df.b dat lev a1 1 a a2 2 a a3 3 a b1 4 b b2 5 b b3 6 b c1 7 c c2 8 c c3 9 c library(reshape) melt(df.a, measure.var = names(df.a), variable_name = lev) lev value 1 a 1 2 a 2 3 a 3 4 b 4 5 b 5 6 b 6 7 c 7 8 c 8 9 c 9 Thanks. __ 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. -- Chuck Cleland, Ph.D. NDRI, Inc. 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 512-0171 (M, W, F) fax: (917) 438-0894 __ 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.