Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8586515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:19:40+00:00 2026-06-11T22:19:40+00:00

I have a data frame called VarChange containing 1005 variables such as: row.name SamplingEvent

  • 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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T22:19:42+00:00Added an answer on June 11, 2026 at 10:19 pm

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 13 quantitative variables in a data.frame (called 'UNCA'). The variables are named
I have a data.frame called mydata and a vector ids containing indices of the
I have a data.frame called series_to_plot.df which I created by combining a number of
I have a data.frame in R; it's called p . Each element in the
I have data frame with some numerical variables and some categorical factor variables. The
I have a data frame like below (20,000 rows by 49 cols). Each row
I have a data frame with two qualitative variables (Q1, Q2) which are both
I have a data.frame , called so_data. Columns 13:23 are list s, which hold
I have a file, called genes.txt , which I'd like to become a data.frame.
Suppose I have a data.frame called SLV and I apply the tail()function. I will

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.