문제

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?

도움이 되었습니까?

해결책

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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top