Skip to content

Column name assignment problem

8 messages · Tal Galili, jim holtman, Steve Murray +1 more

#
Dear all,

I'm trying to assign a name to the fourth column whilst using 'assign', but keep encountering errors. What have I done wrong?!
Error in if (do.NULL) NULL else if (nc> 0) paste(prefix, seq_len(nc),  : 
  argument is not interpretable as logical


Hope someone is able to help.

Thanks for any pointers,

Steve


_________________________________________________________________
[[elided Hotmail spam]]
#
Dear all,

I've been trying to implement the advice given to me, but without much success so far. I thought I'd provide the code in full in the hope that it might make more sense. Just to reiterate, I'm attempting to change the header of the 4th column of every table to "COUNT".


year<- 1951:2000
filelist <- paste("C:\\Data\\arunoff_",year,".txt", sep="")
filelist


# Assign file names to individual objects

table_year=1951

for (i in filelist) {
          assign(paste("arunoff_",table_year,"_temp", sep=""),read.table(file=i, header=TRUE, sep=","))
          print(c("LOADED FILE: ","arunoff_",table_year,"_temp"), quote=FALSE)
          table_year = table_year+1
          }


# RE-FORMAT DATA

# Change names of particular column headings
          colnames(assign(paste("arunoff_",table_year, sep=""))[4],"COUNT")


Any help would be very much appreciated.

Thanks as ever,

Steve

_________________________________________________________________
[[elided Hotmail spam]]
#
Since you want to generate your own variable names instead of using a
list, do the operation in three steps:

x <- get(paste("arunoff_",table_year, sep=''))
colnames(x)[4] <- "COUNT"
assign(paste("arunoff_",table_year, sep=''), x)
On Fri, Mar 27, 2009 at 4:01 PM, Steve Murray <smurray444 at hotmail.com> wrote:

  
    
2 days later
#
Jim and all,

Thanks - I managed to get it working based on your helpful advice.

I'm now trying to do something very similar which simply involves changing the names of the variables in column 1 to make them more succinct. I'm trying to do this via the 'levels' command as I figured that I might be able to apply the character strings in a similar way to how you recommended when dealing with 'colnames'.


# Refine names of rivers to make more succinct
          riv_names <- get(paste("arunoff_",table_year, sep=''))[,1]
          levels(riv_names) <- c("AMAZON", "AMUR", "CONGO", "LENA", "MISSISSIPPI", "NIGER", "NILE", "OB", "PARANA", "YANGTZE", "YENISEI", "ZAMBEZI")
          assign(get(paste("arunoff_",table_year, sep='')[,1], levels(riv_names)))

Error in paste("arunoff_", table_year, sep = "")[, 1] : 
  incorrect number of dimensions

My thinking was to assign the levels of riv_names to column 1 of the table...

Many thanks again for any advice offered,

Steve
#
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])""
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 "                    }"



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
#
Steve Murray wrote:
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)
))

  
    
#
Dear Peter, Jim and all,

Thanks for the information regarding how to structure 'assign' commands. I've had a go at doing this, based on your advice, and although I feel I'm a lot closer now, I can't quite get it to work:

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 1:120) {
        Fekete_table <- get(paste("Fekete_", index$year[i], index$month[i], sep=''))
        colnames(Fekete_table) <- columnnames
            rownames(Fekete_table) <- rnames
        assign(paste("Fekete_",index$year[i], index$month[i], sep=''),
        colnames(Fekete_table))
        }

This assigns the column headings to each table, so that each table doesn't contain data any longer, but simply the column values. I tried inserting assign(colnames(paste("Fekete_"...) but this resulted in the type of error that was mentioned in the previous message. I've run dry of ideas as to how I should restructure the commands, so would be grateful for any pointers.

Many thanks,

Steve


_________________________________________________________________
[[elided Hotmail spam]]