0

I have a data frame called VarChange containing 1005 variables such as:

   row.name   SamplingEvent  Year  Zenaida_macroura  . . . 1005 variables
1  12367      S41            2005  0
2  12369      S42            2005  X
3  12370      S43            2005  4
4  OldSppName SamplingEvent  Year  Zenaida_macroura
5  NewSppName SampEvent      Year  Zenamacr

My goal is to change the column names of the data frame to the row called "NewSppName" (a maximum 8 letters code equivalent of the present variable name). This is needed to in order to not loose track of the various variables in ArcMap (which truncates all variables names to 8 characters).

Every thing looks good (i.e. R outputs the appropriate NewSppNames) when I ask for:

Var['NewSppName',]

But when I use:

colnames(VarChange) <- VarChange['NewSppName', ]

or

colnames(VarChange) <- as.character(VarChange["NewSppName",])

I get the following output:

   row.names  7              Year  8            . . . 1005 variables
1  12367      S41            2005  0
2  12369      S42            2005  X
3  12370      S43            2005  4
4  OldSppName SamplingEvent  Year  Zenaida_macroura
5  NewSppName SampEvent      Year  Zenamacr

95% of the variables do change name to the value in NewSppName but a dozen or so changes to numbers instead of the character name present in NewSppName.

Any reason why? Any solution to this?

JoeBird
  • 65
  • 1
  • 10
  • 1
    Welcome to StackOverflow! Could you provide a reproducible example using `dput`, perhaps of just a sample of the data? (We can't tell what data type each column is from this) – David Robinson Sep 26 '12 at 01:01
  • Are the new column names really a row in the original data? This would seem odd as this would force all the variables to be `character` or `factor`? – mnel Sep 26 '12 at 01:07
  • Note that "SampEvent" has nine characters. – flodel Sep 26 '12 at 01:21

1 Answers1

0

When using indexing you need to use one of logical or numeric or rownames. Just because you have a column named 'row.name' will not tell R to use it for indexing.

VarChange <- read.table(text="   row.name   SamplingEvent  Year  Zenaida_macroura  
1  12367      S41            2005  0
2  12369      S42            2005  X
3  12370      S43            2005  4
4  OldSppName SamplingEvent  Year  Zenaida_macroura
5  NewSppName SampEvent      Year  Zenamacr
", header=TRUE)
> as.character(VarChange["NewSppName",])
[1] "NA" "NA" "NA" "NA"

So instead either use '5' or construct the correct logical vector VarChange$row.names=="NewSppName". You were probably getting different results because you had some different objects on of which was named 'NewSppName' in your workspace. You also need to safely convert to character by sapply()-ing as.character, since they are factors with different levels and a global as.character(.) will not succeed:

> sapply(VarChange[VarChange$row.name=="NewSppName",] ,as.character)
        row.name    SamplingEvent             Year Zenaida_macroura 
    "NewSppName"      "SampEvent"           "Year"       "Zenamacr" 

> colnames(VarChange) <- sapply(VarChange[5,] ,as.character)
> VarChange
  NewSppName     SampEvent Year         Zenamacr
1      12367           S41 2005                0
2      12369           S42 2005                X
3      12370           S43 2005                4
4 OldSppName SamplingEvent Year Zenaida_macroura
5 NewSppName     SampEvent Year         Zenamacr
IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • Thank you DWin. The command line "colnames(VarChange) <- sapply(VarChange[5,] ,as.character)" worked beautifully. The logical vector VarChange$row.names=="NewSppName" was actually not outputting the intended result but using a slight variation of your suggestion "colnames(VariableChange) <- sapply(VariableChange["NewSppName", ] ,as.character)" I was able to get the expected output. I do not believe the name 'NewSppName' was a different object in the workspace. In any case: things are working now. Thanks again! – JoeBird Sep 26 '12 at 17:06