How to name column header for first column ?
2
3
Entering edit mode
6.7 years ago
Björn ▴ 110

I have a dataset with a missing column header for first column. How to name it so that I can find it using

a$

screenshot

I used following command but got error

a$miRNAs<-row.names(a)

Warning message:
In a$miRNAs <- row.names(a) : Coercing LHS to a list

How to find the solution?

r dplyr • 18k views
ADD COMMENT
1
Entering edit mode

It is not a data frame column, these are the row names:

rownames( a );

You can't assign a name to the row names.

ADD REPLY
0
Entering edit mode

If you are trying to access a single column from a dataframe, you should use this syntax instead: a[["column_name"]]. There are methods for renaming columns listed here

ADD REPLY
5
Entering edit mode
6.7 years ago
zx8754 12k

It is rownames not a column. Since you have mentioned dplyr, we can convert it to columns using packages from tidyverse, here is an example:

# example dataset with rownames
a <- mtcars[1:3, 1:3]
a
#                mpg cyl disp
# Mazda RX4     21.0   6  160
# Mazda RX4 Wag 21.0   6  160
# Datsun 710    22.8   4  108

library(dplyr)
library(tibble)

a <- a %>% 
  rownames_to_column(var = "myName")

a
#          myName  mpg cyl disp
# 1     Mazda RX4 21.0   6  160
# 2 Mazda RX4 Wag 21.0   6  160
# 3    Datsun 710 22.8   4  108
ADD COMMENT
2
Entering edit mode
6.7 years ago
> test=mtcars[1:3, 1:3]
> test
                        mpg cyl disp
Mazda RX4         Mazda RX4   6  160
Mazda RX4 Wag Mazda RX4 Wag   6  160
Datsun 710       Datsun 710   4  108
> cbind(cars=rownames(test),test)
                       cars  mpg cyl disp
Mazda RX4         Mazda RX4 21.0   6  160
Mazda RX4 Wag Mazda RX4 Wag 21.0   6  160
Datsun 710       Datsun 710 22.8   4  108
ADD COMMENT

Login before adding your answer.

Traffic: 1713 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6