On Fri, 17 Nov 2006, Tom McCallum wrote: > Hi, > > I have been studying the R manual on lists but cannot seem to create a > simple R list in C - I keep on getting "'dimnames' applied to non-array" > whenever I attempt to assign names to the list elements:
Maybe: setAttrib( priceList, R_NamesSymbol, dimnames ); why should a list have dimnames? In addition, your dimnames is a list, and likely ought to be a character vector. That way you can get away with many fewer PROTECT(). Roger > > Wanted output a list structure something like > [ type="Bid", price=2.0, volume=1000 ] > > I can get up to a list of > [ "Bid2, 2.0, 1000 ] > > But for the life of me I can't assign the names to the list items - any > suggestions? > > Below is what I already have - any comments on things I don't really need > to do as well in terms of creating extra string objects etc are also > helpful. > > Many thanks > > Tom > > > ======EXTRACT========= > > > SEXP typeStr, volumeStr, priceStr, typeValStr; > SEXP priceList; > > // create main list of vectors (unspecific items ) > PROTECT( priceList = allocVector( VECSXP, 3 ) ); > > // for each item create a label and attach value > PROTECT( typeValStr = allocVector( STRSXP, 1 ) ); > SET_STRING_ELT( typeValStr, 0, > mkChar( getPriceDataTypeAsString( p.getData().getType() ).c_str() ) ); > SET_STRING_ELT( priceList, 0, typeValStr) ; > > SEXP priceSXP; > PROTECT( priceSXP = allocVector( REALSXP, 1) ); > REAL(priceSXP)[0] = p.getData().getPrice(); > SET_VECTOR_ELT( priceList, 1, priceSXP); > > SEXP volumeSXP; > PROTECT( volumeSXP = allocVector( REALSXP, 1 ) ); > REAL(volumeSXP)[0] = p.getData().getVolume(); > SET_VECTOR_ELT( priceList, 2, volumeSXP); > > ********WORKS UP TO HERE************* > > // add the names of the list items (type,price,volume) > SEXP dimnames; > PROTECT( dimnames = allocVector( VECSXP, 3 ) ); > > // first create string objects > PROTECT( typeStr = allocVector( STRSXP,1 ) ); > SET_STRING_ELT( typeStr, 0, mkChar("type") ); > > PROTECT( priceStr = allocVector( STRSXP,1 ) ); > SET_STRING_ELT( priceStr, 0, mkChar("price") ); > > PROTECT( volumeStr = allocVector( STRSXP,1 ) ); > SET_STRING_ELT( volumeStr, 0, mkChar("volume") ); > > // assign string objects to vector > SET_VECTOR_ELT( dimnames, 0, typeStr ); > SET_VECTOR_ELT( dimnames, 1, priceStr); > SET_VECTOR_ELT( dimnames, 2, volumeStr ); > > // assign vector to original list of data > setAttrib( priceList, R_DimNamesSymbol, dimnames ); <----- I think this > is wrong but I don;t know what it should be > > ===========END EXTRACT=============== > > -- Roger Bivand Economic Geography Section, Department of Economics, Norwegian School of Economics and Business Administration, Helleveien 30, N-5045 Bergen, Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43 e-mail: [EMAIL PROTECTED] ______________________________________________ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel