# Table Construction from calculations

3 messages
Open this post in threaded view
|

## Table Construction from calculations

 Hi- I am trying to create a table of values by adding  pairs of vectors, but am running into some problems.  The problem is best expressed by a simple example. Starting with a data table "basis":   atom   x   y   z 1   Cu 0.0 0.0 0.0 2   Cu 0.5 0.5 0.5 I want to add 0.5 0.5 0.5  (and also the 0 0 0 but it wouldn't change the values below so I won't refer to it in the rest of the example) to a list of vectors in the form of:  > latpoints    V1 V2 V3 1   0  0  0 2   0  0  1 3   0  0  2 4   0  0  3 5   0  1  1 so that I end up with a table such as: V1  V2  V3 0.5 0.5 0.5 0.5 0.5 1.5 0.5 0.5 2.5 0.5 0.5 3.5 0.5 1.5 1.5 I've tried many variations on the following: (not just cat, but most of the data/data.table options)  test = for(i in 1:5) {cat(basis[1,2:4] + latticemultipliers[i,], append=TRUE)} However, I either end up with an error telling me that cat doesn't handle "type 'list' " or with a table with length of 1 such as:      x    y    z 2  0.5 1.5 1.5 Which is simply the last value that the loop calculates. Does anyone know what function handles lists of the form I am using, or have a better suggestion on how to get the form that I want. Thanks in advance, Seth Imhoff ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: Table Construction from calculations

 Your "data table" basis is actually a dataframe, whose first column is non-numeric.  That's what is causing the problem.   Try removing the first column of the dataframe before adding the row to your matrix: test <- latpoints + basis[2, -1] -Christos Christos Hatzis, Ph.D. Nuvera Biosciences, Inc. 400 West Cummings Park Suite 5350 Woburn, MA 01801 Tel: 781-938-3830 www.nuverabio.com > -----Original Message----- > From: [hidden email] > [mailto:[hidden email]] On Behalf Of Seth Imhoff > Sent: Friday, March 09, 2007 9:23 PM > To: [hidden email] > Subject: [R] Table Construction from calculations > > Hi- > > I am trying to create a table of values by adding  pairs of > vectors, but am running into some problems.  The problem is > best expressed by a simple example. > > Starting with a data table "basis": >   atom   x   y   z > 1   Cu 0.0 0.0 0.0 > 2   Cu 0.5 0.5 0.5 > > I want to add 0.5 0.5 0.5  (and also the 0 0 0 but it > wouldn't change the values below so I won't refer to it in > the rest of the example) to a list of vectors in the form of: >  > latpoints >    V1 V2 V3 > 1   0  0  0 > 2   0  0  1 > 3   0  0  2 > 4   0  0  3 > 5   0  1  1 > > so that I end up with a table such as: > V1  V2  V3 > 0.5 0.5 0.5 > 0.5 0.5 1.5 > 0.5 0.5 2.5 > 0.5 0.5 3.5 > 0.5 1.5 1.5 > > I've tried many variations on the following: (not just cat, > but most of the data/data.table options) > >  test = for(i in 1:5) {cat(basis[1,2:4] + > latticemultipliers[i,], append=TRUE)} > > However, I either end up with an error telling me that cat > doesn't handle "type 'list' " or with a table with length of > 1 such as: >      x    y    z > 2  0.5 1.5 1.5 > > Which is simply the last value that the loop calculates. > > Does anyone know what function handles lists of the form I am > using, or have a better suggestion on how to get the form that I want. > > Thanks in advance, > Seth Imhoff > > ______________________________________________ > [hidden email] 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. > > ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: Table Construction from calculations

 In reply to this post by Seth Imhoff On Mar 9, 2007, at 9:23 PM, Seth Imhoff wrote: > I am trying to create a table of values by adding  pairs of   > vectors, but > am running into some problems.  The problem is best expressed by a > simple example. > > Starting with a data table "basis": >   atom   x   y   z > 1   Cu 0.0 0.0 0.0 > 2   Cu 0.5 0.5 0.5 > > I want to add 0.5 0.5 0.5  (and also the 0 0 0 but it wouldn't change > the values below so I won't refer to it in the rest of the example)   > to a > list of vectors in the form of: >> latpoints >    V1 V2 V3 > 1   0  0  0 > 2   0  0  1 > 3   0  0  2 > 4   0  0  3 > 5   0  1  1 > > so that I end up with a table such as: > V1  V2  V3 > 0.5 0.5 0.5 > 0.5 0.5 1.5 > 0.5 0.5 2.5 > 0.5 0.5 3.5 > 0.5 1.5 1.5 > > I've tried many variations on the following: (not just cat, but   > most of > the data/data.table options) > >  test = for(i in 1:5) {cat(basis[1,2:4] + latticemultipliers[i,], > append=TRUE)} > > However, I either end up with an error telling me that cat doesn't > handle "type 'list' " or with a table with length of 1 such as: >      x    y    z > 2  0.5 1.5 1.5 Is this what you want?  > (latpoints <- data.frame(matrix(c(0,0,0,0,0,1,0,0,2,0,0,3,0,1,1),   nrow = 5, byrow = T)))    X1 X2 X3 1  0  0  0 2  0  0  1 3  0  0  2 4  0  0  3 5  0  1  1  > (latpoints <- latpoints + c(0.5, 0.5, 0.5))     X1  X2  X3 1 0.5 0.5 0.5 2 0.5 0.5 1.5 3 0.5 0.5 2.5 4 0.5 0.5 3.5 5 0.5 1.5 1.5 This is an important feature of R called "vectorization" (see, .e.g,   cran.r-project.org/doc/contrib/Paradis-rdebuts_en.pdf or   www.ms.washington.edu/stat390/winter07/R_primer.pdf) which allows you   do avoid writing loops. _____________________________ Professor Michael Kubovy University of Virginia Department of Psychology USPS:     P.O.Box 400400    Charlottesville, VA 22904-4400 Parcels:    Room 102        Gilmer Hall          McCormick Road    Charlottesville, VA 22903 Office:    B011    +1-434-982-4729 Lab:        B019    +1-434-982-4751 Fax:        +1-434-982-4766 WWW:    http://www.people.virginia.edu/~mk9y/______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.