本文介绍了如何设置使用管道%&%;%运算符传递的数据帧的行名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框,正在使用 reshape2 包进行 dcast 删除,我想删除

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.

原始数据帧,在 dcast 之前:

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 函数删除表:

 library(reshape2)

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

dcast 之后,包裹在我的轴中函数:

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

我希望ID_full成为行名,并不再作为列存在,在 dcasting 之后通过管道传输。我可以在多行代码中执行此操作,每次都替换数据帧,但是我想使用%>%运算符来全部完成操作。

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!

更新:

感谢您的回答:

这个表达式很好地工作:

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


推荐答案

会这样做吗?

iris %>% `rownames<-`(seq_len(nrow(iris)))

这篇关于如何设置使用管道%&%;%运算符传递的数据帧的行名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 16:25