Question: How to name column header for first column ?
1
gravatar for Björn
6 months ago by
Björn30
Björn30 wrote:

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?

dplyr R • 336 views
ADD COMMENTlink modified 6 months ago by cpad01129.0k • written 6 months ago by Björn30
1

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 REPLYlink modified 6 months ago • written 6 months ago by h.mon19k

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 REPLYlink written 6 months ago by steve1.7k
3
gravatar for zx8754
6 months ago by
zx87545.0k
London
zx87545.0k wrote:

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 COMMENTlink written 6 months ago by zx87545.0k
1
gravatar for cpad0112
6 months ago by
cpad01129.0k
India
cpad01129.0k wrote:
> 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 COMMENTlink written 6 months ago by cpad01129.0k

Base ftw :). For simple task like this I would stick with base, too. But OP tagged the post with dplyr.

ADD REPLYlink written 6 months ago by zx87545.0k
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1714 users visited in the last hour