Question: How to name column header for first column ?
1
gravatar for Björn
9 months ago by
Björn40
Björn40 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 • 400 views
ADD COMMENTlink modified 9 months ago by cpad011210k • written 9 months ago by Björn40
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 9 months ago • written 9 months ago by h.mon22k

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 9 months ago by steve1.8k
3
gravatar for zx8754
9 months ago by
zx87546.1k
London
zx87546.1k 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 9 months ago by zx87546.1k
1
gravatar for cpad0112
9 months ago by
cpad011210k
India
cpad011210k 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 9 months ago by cpad011210k

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

ADD REPLYlink written 9 months ago by zx87546.1k
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: 1160 users visited in the last hour