How to set the row names of a data frame passed on with the pipe %>% operator?
Asked Answered
H

5

33

I have a data frame which I am dcasting using the reshape2 package, and I would like to remove the first column and have it become the row names of the data frame instead.

Original dataframe, before dcast:

> corner(df)

ID_full      gene cpm
1  S36-A1   DDX11L1   0
2  S36-A1    WASH7P   0
3  S36-A1 MIR1302-2   0
4  S36-A1   FAM138A   0
5  S36-A1     OR4F5   0

pivot function to dcast the table:

 library(reshape2)

 pivot <- function(x){
             castTable <- x %>% dcast(ID_full ~ gene, value.var="cpm")
             }

After dcast, wrapped in my pivot function:

> corner(df)

ID_full 1060P11.3 A1BG A1BG-AS1 A1CF
1  S36-A1         0    0        0    0
2 S36-A10         0    0        0    0
3 S36-A11         0    0        0    0
4 S36-A12         0    0        0    0
5  S36-A2         0    0        0    0

I'd like ID_full to become the rownames, and to cease existing as a column, piped after dcasting. I can do this in several lines, replacing the data frame each time, but I'd like to do it all using the %>% operator.

The best attempt I can think of would involve something like this, but obviously it doesn't work:

library(dplyr)

df <- df %>% pivot(.) %>% with(., row.names=df[,1])

I'd appreciate any suggestions... this nuisance is driving me crazy!

UPDATE:

Thanks for your answers:

This expression works nicely:

df <- df %>% pivot(.) %>% `rownames<-`(.[,1]) %>% select(-ID_full)

> corner(df)

        1060P11.3 A1BG A1BG-AS1 A1CF        A2M
S36-A1          0    0        0    0    0.00000
S36-A10         0    0        0    0    0.00000
S36-A11         0    0        0    0    0.00000
S36-A12         0    0        0    0    1.62189
S36-A2          0    0        0    0 1170.95000
Horseshoe answered 20/2, 2016 at 1:55 Comment(2)
dput() > console output. Where the @#$@# is corner() from? ?rename ; ?rownames.Layer
?tibble::column_to_rownames.Cinthiacintron
G
34

with the later version of tibble, a more elegant solution exists:

df <- df %>% pivot(.) %>% tibble::column_to_rownames('ID_full')

Importantly, it works also when the column to turn to the rowname is passed as a variable, which is super-convenient, when inside the function!

Gaut answered 19/3, 2018 at 12:43 Comment(0)
V
36

will this do?

iris %>% `rownames<-`(seq_len(nrow(iris)))
Volcanic answered 20/2, 2016 at 2:1 Comment(0)
G
34

with the later version of tibble, a more elegant solution exists:

df <- df %>% pivot(.) %>% tibble::column_to_rownames('ID_full')

Importantly, it works also when the column to turn to the rowname is passed as a variable, which is super-convenient, when inside the function!

Gaut answered 19/3, 2018 at 12:43 Comment(0)
T
21

You can use the magrittr alias set_rownames:

df %>% set_rownames(.$ID_full)
Timpani answered 15/6, 2019 at 19:15 Comment(0)
I
0
df <- data.frame(x=1:2,y=3:4,k=c('a','b')) 
df %>% data.frame(row.names = 'k') # remove column k
df %>% data.frame(row.names = 3) # remove column k
df %>% data.frame(row.names = .$k) # keep column k
df %>% data.frame(row.names = .[[3]]) # keep column k
df %>% data.frame(row.names = c('A','B')) # use any other vector as rowname
Inappreciable answered 13/10, 2023 at 0:18 Comment(0)
J
0

This should have been a comment, but I do not yet have enough reputation, so I'll leave it as an answer for now. Combining Henrik's idea into Jelena's solution makes it work also for a data frame that already has row names (while still dropping the column from where the row names are taken):

df <- df %>% pivot(.) %>%
    magrittr::set_rownames(NULL) %>%
    tibble::column_to_rownames('ID_full')
Jadda answered 24/6, 2024 at 16:10 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.