转置数据帧 [英] Transpose a data frame

查看:59
本文介绍了转置数据帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要转置一个大数据框,所以我使用了:

I need to transpose a large data frame and so I used:

df.aree <- t(df.aree)
df.aree <- as.data.frame(df.aree)

这就是我得到的:

df.aree[c(1:5),c(1:5)]
                         10428        10760        12148        11865
    name                M231T3       M961T5       M960T6      M231T19
    GS04.A        5.847557e+03 0.000000e+00 3.165891e+04 2.119232e+04
    GS16.A        5.248690e+04 4.047780e+03 3.763850e+04 1.187454e+04
    GS20.A        5.370910e+03 9.518396e+03 3.552036e+04 1.497956e+04
    GS40.A        3.640794e+03 1.084391e+04 4.651735e+04 4.120606e+04    

我的问题是新的列名(10428、10760、12148 ,11865),因为我需要使用第一行作为列名,因此需要消除。

My problem is the new column names(10428, 10760, 12148, 11865) that I need to eliminate because I need to use the first row as column names.

我尝试使用 col.names()函数,但我没有获得所需的东西。

I tried with col.names() function but I haven't obtain what I need.

您有任何建议吗?

编辑

感谢您的建议!!!使用它,我得到:

Thanks for your suggestion!!! Using it I obtain:

df.aree[c(1:5),c(1:5)]
                        M231T3       M961T5       M960T6      M231T19
    GS04.A        5.847557e+03 0.000000e+00 3.165891e+04 2.119232e+04
    GS16.A        5.248690e+04 4.047780e+03 3.763850e+04 1.187454e+04
    GS20.A        5.370910e+03 9.518396e+03 3.552036e+04 1.497956e+04
    GS40.A        3.640794e+03 1.084391e+04 4.651735e+04 4.120606e+04
    GS44.A        1.225938e+04 2.681887e+03 1.154924e+04 4.202394e+04

现在我需要转换因子列中的行名(GS ..)。...

Now I need to transform the row names(GS..) in a factor column....

推荐答案

您最好不要在名称列位于其中的情况下转置data.frame -然后所有数字值都将转换为字符串!

You'd better not transpose the data.frame while the name column is in it - all numeric values will then be turned into strings!

这里是一种将数字保留为数字的解决方案:

Here's a solution that keeps numbers as numbers:

# first remember the names
n <- df.aree$name

# transpose all but the first column (name)
df.aree <- as.data.frame(t(df.aree[,-1]))
colnames(df.aree) <- n
df.aree$myfactor <- factor(row.names(df.aree))

str(df.aree) # Check the column types

这篇关于转置数据帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆