[R] Column name assignment problem

Peter Dalgaard P.Dalgaard at biostat.ku.dk
Mon Mar 30 15:10:52 CEST 2009


Steve Murray wrote:
> Dear all,
> 
> Apologies for yet another question (!). Hopefully it won't be too tricky to solve. I am attempting to add row and column names (these are in fact numbers) to each of the tables created by the code (120 in total).
> 
> 
> # Create index of file names
> files <- print(ls()[1:120], quote=FALSE)  # This is the best way I could manage to successfully attribute all the table names to a single list - I realise it's horrible coding (especially as it relies on the first 120 objects stored in the memory actually being the objects I want to use)...
> 
> files
>   [1] "Fekete_198601" "Fekete_198602" "Fekete_198603" "Fekete_198604"
>   [5] "Fekete_198605" "Fekete_198606" "Fekete_198607" "Fekete_198608"
>   [9] "Fekete_198609" "Fekete_198610" "Fekete_198611" "Fekete_198612"
>   [13] "Fekete_198701" "Fekete_198702" "Fekete_198703" "Fekete_198704"
>   [17] "Fekete_198705" "Fekete_198706" "Fekete_198707" "Fekete_198708" ...[truncated - there are 120 in total]
> 
> 
> # Provide column and row names according to lat/longs.
> 
> rnames <- sprintf("%.2f", seq(from = -89.75, to = 89.75, length = 360))
> columnnames <- sprintf("%.2f", seq(from = -179.75, to = 179.75, length = 720))
> 
> for (i in files) {
>         assign(colnames((paste(Fekete_",index$year[i], index$month[i])", sep='')), columnnames)
>                 assign(rownames(paste("rownames(Fekete_",index$year[i], index$month[i],")", sep=''), rnames))
>                     }
> 
> 
> Error: unexpected string constant in:
> "for (i in files) {
> assign(colnames((paste(Fekete_",index$year[i], index$month[i])""
>>                 assign(rownames(paste("rownames(Fekete_",index$year[i], index$month[i],")", sep=''), rnames))
> Error in if (do.NULL) NULL else if (nr> 0) paste(prefix, seq_len(nr),  : 
>   argument is not interpretable as logical
> In addition: Warning message:
> In if (do.NULL) NULL else if (nr> 0) paste(prefix, seq_len(nr),  :
>   the condition has length> 1 and only the first element will be used
>>                     }
> Error: unexpected '}' in "                    }"


The generic issue here (read: I can't really be bothered to do your
problem in all details...) is that you cannot use assignment forms like

foo(x) <- bar

while accessing x via a character name. That is

a <- "plugh!"
assign(foo(a), bar)

and

foo(get(a)) <- bar

are both wrong.

You need to do it in steps, like

x <- get(a)
foo(x) <- bar
assign(a, x)

or, not really any prettier

eval(substitute(
   foo(x) <- bar, list(x=as.name(a)
))


> 
> 
> Is there a more elegant way of creating a list of file names in this case (remember that there are 2 variable parts to each name) which would facilitate the assigning of column and row names to each table? (And make life easier when doing other things with the data, e.g. plotting...!).
> 
> Many thanks once again - the help offered really is appreciated.
> 
> Steve
> 
> 
> _________________________________________________________________
> All your Twitter and other social updates in one place 
> 
> ______________________________________________
> 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.


-- 
   O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark      Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)              FAX: (+45) 35327907




More information about the R-help mailing list