Dear users, Is anyone out there on a Saturday to answer this easy question? I have the "yo" object data in a "list" format:> str(yo)num [1:259, 1:173] 16.3 NA NA NA NA ... - attr(*, "dimnames")=List of 2 ..$ x: chr [1:259] "367319" "367329" "367309" "367339" ... ..$ y: chr [1:173] "780175" "780185" "780195" "780205" ...> length(yo)[1] 44807 where x is the name of the rows and y the name of the columns (coordinates) of a matrix of 44807 values how can I get a data.frame with these three columns: x, y, values Many thanks to the Saturday working people. Javier and I want to produce a data frame -- View this message in context: http://n4.nabble.com/format-from-list-to-data-frame-tp1591773p1591773.html Sent from the R help mailing list archive at Nabble.com.
On Mar 13, 2010, at 11:46 AM, Javier wrote:> > Dear users, > > Is anyone out there on a Saturday to answer this easy question? > > I have the "yo" object data in a "list" format: > >> str(yo) > num [1:259, 1:173] 16.3 NA NA NA NA ... > - attr(*, "dimnames")=List of 2 > ..$ x: chr [1:259] "367319" "367329" "367309" "367339" ... > ..$ y: chr [1:173] "780175" "780185" "780195" "780205" ... >> length(yo) > [1] 44807 > > where x is the name of the rows and y the name of the columns > (coordinates) > of a matrix of 44807 values > > how can I get a data.frame with these three columns: x, y, values?as.data.frame.table > as.data.frame.table(matrix((1:16)/16, ncol=2)) Var1 Var2 Freq 1 A A 0.0625 2 B A 0.1250 3 C A 0.1875 4 D A 0.2500 5 E A 0.3125 6 F A 0.3750 7 G A 0.4375 8 H A 0.5000 9 A B 0.5625 10 B B 0.6250 11 C B 0.6875 12 D B 0.7500 13 E B 0.8125 14 F B 0.8750 15 G B 0.9375 16 H B 1.0000 You will need to relabel the columns but the "names()<-" function will suffice. -- David.> > Many thanks to the Saturday working people. > > Javier > > > > > > > > > and I want to produce a data frame > > -- > View this message in context: http://n4.nabble.com/format-from-list-to-data-frame-tp1591773p1591773.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.David Winsemius, MD West Hartford, CT
On Sat, Mar 13, 2010 at 4:46 PM, Javier <j.perez-barberia at macaulay.ac.uk> wrote:> > Dear users, > > Is anyone out there on a Saturday to answer this easy question? > > I have the "yo" object data in a "list" format: > >> str(yo) > ?num [1:259, 1:173] 16.3 NA NA NA NA ... > ?- attr(*, "dimnames")=List of 2 > ?..$ x: chr [1:259] "367319" "367329" "367309" "367339" ... > ?..$ y: chr [1:173] "780175" "780185" "780195" "780205" ... >> length(yo) > [1] 44807 > > where x is the name of the rows and y the name of the columns (coordinates) > of a matrix of 44807 values > > how can I get a data.frame with these three columns: x, y, values > > Many thanks to the Saturday working people.Let's try with something a bit smaller. Not sure what you mean by list format, your thing looks like a matrix: d=matrix(1:12,ncol=4) dimnames(d) <- list(letters[1:3],letters[9:12]) - hence: > str(d) int [1:3, 1:4] 1 2 3 4 5 6 7 8 9 10 ... - attr(*, "dimnames")=List of 2 ..$ : chr [1:3] "a" "b" "c" ..$ : chr [1:4] "i" "j" "k" "l" > length(d) [1] 12 If I use 'melt' from the 'reshape' package:> require(reshape) > melt(d)X1 X2 value 1 a i 1 2 b i 2 3 c i 3 4 a j 4 5 b j 5 6 c j 6 7 a k 7 8 b k 8 9 c k 9 10 a l 10 11 b l 11 12 c l 12 I think I get pretty much what you want. You might need to set the columns to numeric and maybe change the names. Barry
On Mar 13, 2010, at 10:46 AM, Javier wrote:> > Dear users, > > Is anyone out there on a Saturday to answer this easy question? > > I have the "yo" object data in a "list" format:It is not a list, it is a matrix. The dimnames attribute is a list.>> str(yo) > num [1:259, 1:173] 16.3 NA NA NA NA ... > - attr(*, "dimnames")=List of 2 > ..$ x: chr [1:259] "367319" "367329" "367309" "367339" ... > ..$ y: chr [1:173] "780175" "780185" "780195" "780205" ... >> length(yo) > [1] 44807 > > where x is the name of the rows and y the name of the columns (coordinates) > of a matrix of 44807 values > > how can I get a data.frame with these three columns: x, y, values > > Many thanks to the Saturday working people. > > JavierTry: as.data.frame.table(yo) HTH, Marc Schwartz